How do I use UpdateView?

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


first: user = form.save() saves in the db the form. since there's no pk in the form it creates a new one. what you have to do is probably to check if a user with that username exists and if not create it (for this part check google).

second: to restrict field you have to specify them in the Meta class of the Form (which you didn't show here) check this https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform.


If you are getting new objects in the database instead of updating existing ones then it is likely that you copied and pasted the template for new objects and forgot to change the form's action attribute. This should point to view that does the update either in the form of a hard-coded path or a URL tag ({% url '<name of URLconf>' object.id %).