python - django change password form not raising error -
i have form changing password:
class passwordchangeform(forms.form): old_password = forms.charfield(max_length=20) new_password1 = forms.charfield(max_length=20) new_password2 = forms.charfield(max_length=20) def __init__(self, user, *args, **kwargs): self.user = user super(passwordchangeform, self).__init__(*args, **kwargs) def clean_old_password(self): old_password = self.cleaned_data.get("old_password") if not self.user.check_password(old_password): raise forms.validationerror("your old password wrong") return old_password def clean_new_password2(self): new_password1 = self.cleaned_data.get("new_password1") new_password2 = self.cleaned_data.get("new_password2") if new_password1 , new_password2 , new_password1 != new_password2: raise forms.validationerror("your new passwords didn't match") return new_password2
in view have:
class passwordchangeview(view): form_class = passwordchangeform template_name = 'registration/password_change.html' def get(self, request, *args, **kwargs): form = self.form_class(user=self.request.user) return render(request, self.template_name, {'form': form}) def post(self, request, *args, **kwargs): form = self.form_class(request.post) print("wow") if form.is_valid(): #my logic here return redirect("password_change_successful") return render(request, self.template_name, {'form': form})
but form.is_valid() function not called.. if enter wrong old password or wrong new passwords doesnot raise error.
what wrong in here ?? thank you
i believe misusing class-based views. view should inherit formview
, not simple view
. , should have method signature form_valid(self, form)
, called valid form has been submitted. also, don't need override , post methods. in fact, not idea so. apologize criticism. here how view should like:
class passwordchangeview(formview): form_class = passwordchangeform success_url = reverse("password_change_successful") # if form had no errors, formview redirects url template_name = 'registration/password_change.html' def form_valid(self, form, *args, **kwargs): # print "wow" logger.debug("wow, user has sent valid form!") return super(passwordchangeview, self).form_valid(form, *args, **kwargs) # def get(...) # there no need override get(), formview # def post(...) # same story post, if form valid, # form_valid() called, otherwise user stay on # same page, same form displaying validation errors
Comments
Post a Comment