How to update django auth User using a custom form

I am using Django's built in authentication to manage users on a social media website. I am using a one-to-one relationship to attach a profile to each user. I can update all the parts of the profile I have attached using an UpdateView. However I don't know how to do that with Django's built in User. So I created a form that uses the _meta class. I have gotten to the point where my form will add a new user instead of update the current one. I was hoping one of you could help me fix my code. Thanks in advance for any help you can offer

views.py

class PrivProfileUpdate(View):
    form_class = UserUpdateForm
    template_name = 'user/user_form.html'

    #display a blank form
    def get(self, request, pk):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    #proces form data
    def post(self, request, pk):
        form = self.form_class(request.POST)
        user = User.objects.get(pk=pk)
        if form.is_valid():

            user = form.save(commit=True)

            print("we are trying to save")
            #cleaned (normalized) data
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            email = form.cleaned_data['email']
            user.set_password(password) #this is the only way to change a password because of hashing
            user.save()

        return render(request, self.template_name,{'form': form})

forms.py

class UserUpdateForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['username', 'email', 'password', 'first_name', 'last_name']

SOLUTION:

in views.py

class PrivProfileUpdate(UpdateView):
    model = User
    form_class = UserUpdateForm
    template_name = 'user/user_form.html'

    def form_valid(self, form):
        user = form.save(commit=True)
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()
        return redirect('user:index')

Upvotes: 2

Views: 2798

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

There's nothing special about the User class here. Just as with any other model, to update an existing instance you pass it as the instance argument to the form.

However, you do not actually need to do this at all yourself. You should be using an UpdateView, which does this for you; then you do not need to define get and post. The only method you need to define here is form_valid, to set the password:

class PrivProfileUpdate(UpdateView):
    form_class = UserUpdateForm
    template_name = 'user/user_form.html'

    def form_valid(self, form):
        user = form.save(commit=True)
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()
        return HttpResponseRedirect(self.get_success_url())

Upvotes: 3

Related Questions