【问题标题】:django change password form not raising errordjango更改密码表单不引发错误
【发布时间】:2015-07-08 11:19:47
【问题描述】:

我有一个更改密码的表格:

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 is 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 and new_password2 and new_password1 != new_password2:
        raise forms.ValidationError("Your new passwords didn't match")
    return new_password2

在我看来,我有:

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})

但是我的 form.is_valid() 函数没有被调用。即使我输入了错误的旧密码或错误的新密码,它也不会引发错误。

这里有什么问题?? 谢谢

【问题讨论】:

  • 我不确定你为什么要自己定义这些。 Django 已经包含了 forms and views 来实现这个功能,您所要做的就是提供模板并将它们链接到 URL。
  • 如果我想这样做怎么办?有什么办法吗??
  • 当然有。但是看看你是如何覆盖表单的签名的,然后看看你是如何在post 方法中实例化它的。我想说的是,即使你不想使用提供的 auth 东西,你至少应该为你正在做的事情使用适当的类:你应该使用 ModelForm 和 UpdateView。
  • 你能详细说明一下吗..
  • 我正在为 api 写这个,所以我不想使用更新视图。我可以用视图来做吗?

标签: python django forms validation


【解决方案1】:

我相信您在滥用基于类的视图。 你的视图应该继承自FormView,而不是简单的View。 它应该有一个签名为form_valid(self, form) 的方法,一旦提交了有效的表单,就会调用该方法。 此外,您不需要重写 get 和 post 方法。事实上,这样做并不是一个好主意。 我为批评道歉。 以下是您的视图的外观:

class PasswordChangeView(FormView):
    form_class = PasswordChangeForm
    success_url = reverse("password_change_successful")  # if form had no errors, FormView redirects to this url
    template_name = 'registration/password_change.html'

    def form_valid(self, form, *args, **kwargs):
    # print "wow"
    logger.debug("Wow, some user has sent a valid form!")
    return super(PasswordChangeView, self).form_valid(form, *args, **kwargs)

    # def get(...)
    # there is really no need to override get(), FormView will do everything for you 

    # def post(...)
    # same story with the post, if your form was valid,
    # form_valid() will be called, otherwise user will stay on the
    # same page, with the same form displaying the validation errors

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多