python - Problem with form in Django -
i'm trying implement form let user change password.
here code:
forms.py
class changepasswordform(forms.form): password1 = forms.charfield(widget = forms.passwordinput(), required = true) password2 = forms.charfield(widget = forms.passwordinput(), required = true) def clean_password2(self): password1 = self.cleaned_data.get("password1", "") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.validationerror("the 2 password fields didn't match.") return password2 def save(self, *args, **kw): self.user.set_password(self.cleaned_data['password1']) print "pass setted." self.user.save()
views.py
def change_password(request): form = changepasswordform() if request.method == 'post': form = changepasswordform(request.post, instance = request.user) if form.is_valid(): password1 = form.cleaned_data['password1']; password2 = form.cleaned_data['password2']; print password1 + " == " + password2 form.save() messages.success(request, "password updated!") return httpresponseredirect('') return render(request, 'change_password.html', {'form': form})
it doesn't work because user not defined. problem how pass user form. ideas? thanks.
just make parameter save
method.
def save(self, user): user.set_password(self.cleaned_data['password1']) user.save() ... form.save(request.user)
Comments
Post a Comment