Search code examples
pythondjangodjango-formsdjango-viewsdjango-users

Django User Update Form and View


I am very early on in my Django/Python development journey, most things I have been able to slowly figure out after a few hours/days of head scratching and trial/error. I now have the commonly asked question that I cannot get working correctly:

How Do I Create a User Profile Update View/Form?

I have hacked on several solutions from Stack Overflow, and just cannot figure out what I am doing wrong thus far. Here is the initial version of my poor attempt using Django 1.9:

#forms.py
class profileForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email']

#views.py
@login_required
def profile(request):
    if request.method == 'POST':
        form = profileForm(data=request.POST, instance=request.user)
        update = form.save(commit=False)
        update.user = request.user
        update.save()
    else:
        form = profileForm(instance=request.user)

    return render(request, 'profile.html', {'form': form})

#urls.py
urlpatterns = [
    url(r'^dashboard/$', views.dashboard, name='dashboard'),
    url(r'^dashboard/profile$', views.profile, name='profile'),
]

#profile.html
<form action="." method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Update Profile</button>
</form>

So the current behaviour of my update user profile view is to not update any of the specified fields in the form on POST, but the page does load to a different URL. For example the URL for the profile update form is /dashboard/profile which redirects to /dashboard on form submission. If I manually add email/first_name/last_name in the Django admin, it is displayed correctly in my form... I just cannot get it to save updated data.

I did have a version of the above code that was giving me username errors also, which I am guessing might have been close to a solution? Eventually I would like to use the email address as the username, but I would like to figure that out for myself once I have an ultra-simple working profile update form.

I apologise in advance for the silly question, I have Googled around and attempted other solutions but I either cannot get the same initial errors or cannot get a working version as detailed.

Thanks all and have a great day/night!

EDIT:

I have also tried this solution How to update User object without creating new one?, which also has the same behaviour: not update any of the specified fields in the form on POST, but the page does load to a different URL... which confuses me more, maybe the issue is with my urls.py instead?


Solution

  • There's nothing specific to users here. You have two problems: the first is because of your urls. Your profile view does not end in a slash, so the form's action of "." sends it to the parent, "/dashboard", and it does not go through the processing code at all.

    url(r'/dashboard/profile/$'...)
    

    Secondly, the model will not be updated because you have not called the form's valdation method.

    if request.method == 'POST':
        form = profileForm(data=request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('somewhere')
    

    Note there is no reason to use the commit=False stuff here; the instance is already the user.