I\'m using an UpdateView to update a series of fields. However, I only want fields that have been modified to be saved to the database. If a value was not provided for a fie
i'm using a custom hash during update process to encrypt passwords. When i visit the edit page and hit update button, the old password in its current encrypted form gets re-encrypted hence losing the old password
I would handle that by not including password
itself in the form. Instead, I would add a new field (something like new_password
) to allow entering a new password. Then, in your is_valid
method, set the password to the hashed value of that field if there's content.
You should also use the sensitive value filtering tools to prevent user passwords from showing up in emailed error reports.
class UpdateForm(forms.ModelForm):
class Meta:
model = user
fields = ('first_name', 'last_name', 'email', 'username')
new_password = forms.CharField(required=False, widget=forms.widgets.PasswordInput)
And then in your view:
@sensitive_variables('new_password')
@sensitive_post_parameters('new_password')
def form_valid(self, form):
clean = form.cleaned_data
new_password = clean.get('new_password')
if new_password:
#encrypt plain password
form.instance.password = hash_password(new_password)
return super(AccountUpdate, self).form_valid(form)
The easiest way would be to pre-populate the fields with what's already there.
Do this on the template with {{ account.name }}
or whatever.