How do I use UpdateView?

aled1027 picture aled1027 · Aug 1, 2013 · Viewed 14.7k times · Source

I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form.

Here is my views.py:

class RegistrationView(FormView):
    form_class = RegistrationForm 
    template_name = "register.html"
    success_url = "/accounts/profile/" 

    def form_valid(self, form):
        if form.is_valid:
            user = form.save() 
            user = authenticate(username=user.username, password=form.cleaned_data['password1'])
            login(self.request, user)
            return super(RegistrationView, self).form_valid(form) #I still have no idea what this is

class UserUpdate(UpdateView):
    model = User
    form_class = RegistrationForm
    fields = ['username', 'first_name']
    template_name = "update.html"
    success_url = "/accounts/profile/" 

and urls.py

url(r'^create/$', RegistrationView.as_view(), name="create-user"), 
url(r'^profile/(?P<pk>\d+)/edit/$', UserUpdate.as_view(), name="user-update"), 

How do I properly use UpdateView?

Answer

Staccato picture Staccato · Aug 7, 2013

Problem 1. The user is not being updated because you are using the same form (RegistrationForm) to do your updates and to create new users.

Problem 2. Forms belong in a file of their own called forms.py. My suggested refactoring:



    #forms.py
    #place(forms.py) this in the same directory as views.py

    class UpdateForm(forms.ModelForm):
    #form for updating users
    #the field you want to use should already be defined in the model
    #so no need to add them here again DRY
        class Meta:
            model = User
            fields = ('field1', 'field2', 'field3',)

    #views.py
    #import your forms
    from .forms import UpdateForm
    #also import your CBVs
    from django.views.generic import UpdateView

    class UserUpdate(UpdateView):  
        context_object_name = 'variable_used_in `update.html`'
        form_class = UpdateForm
        template_name = 'update.html'
        success_url = 'success_url'

        #get object
        def get_object(self, queryset=None): 
            return self.request.user

        #override form_valid method
        def form_valid(self, form):
            #save cleaned post data
            clean = form.cleaned_data 
            context = {}        
            self.object = context.save(clean) 
            return super(UserUpdate, self).form_valid(form)    

slightly elegant urls.py



    #urls.py
    #i'm assuming login is required to perform edit function
    #in that case, we don't need to pass the 'id' in the url. 
    #we can just get the user instance
    url(
        regex=r'^profile/edit$',
        view= UserUpdate.as_view(),
        name='user-update'
    ),

You left out a lot of info so not really sure what your setup is.My solution is based on the assumption that you have Django 1.5. You can learn more about handling forms with CBVs