简体   繁体   中英

Django 1.7 - update a user record using a form

I have created a form to update the existing user profile. But when i save the form it shows the error user already exists.

I used another approach by getting the user profile and then updating each field, but in that case each field has to be validated?

Any clue how to save the form as an update not as a new entry?

I suggest using UpdateView , one of Django's class-based-views for generic editing:

class django.views.generic.edit.UpdateView

A view that displays a form for editing an existing object, redisplaying the form with validation errors (if there are any) and saving changes to the object. This uses a form automatically generated from the object's model class (unless a form class is manually specified).

I managed to get the answer, i imported the form to

import the user that i want to edit

u = User.objects.get(username = user_name)

creating the form with values from existing in database and updating with values from POST

user_form = UserEditForm(request.POST,instance=u)

save the form, since it already has existing record it will update

user_form.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM