django中的form.is_valid()总是返回False

这个问题是由于html模板中提交的参数名字和forms.py中的变量名字不一致导致的:

下面是正确示范:

templates/pwdreset.html

<h3 class="no-margins">重置修改</h3>
<p class="m-t-md">密码重置修改</p>
<form method="post" action="{% url 'modify_pwd' %}" autocomplete="off">
    <input name='email' type="text" value="{{ email }}" hidden />

    <input name='password1' type="password" class="form-control pword m-b" placeholder="密码" />
    {% if modifypwd_form.errors.password %}
        <span class="help-block m-b-none"> {{ modifypwd_forms.errors.password.as_text }}</span>
    {% endif %}
    <input name='password2' type="password" class="form-control pword m-b" placeholder="重复密码" />
    {% if modifypwd_form.errors.re_password %}
        <span class="help-block m-b-none"> {{ modifypwd_form.errors.re_password.as_text }}</span>
    {% endif %}

    {% if msg %}
        <div class="alert alert-danger" style="padding: 5px;">
            {{ msg }}
        </div>
    {% endif %}

    {% csrf_token %}

    <a href="/user/login/">返回登录</a>
    <button type="submit" class="btn btn-success btn-block">修 改</button>
</form>

user/forms.py

from django import forms
from captcha.fields import CaptchaField
class ForgetPwdForm(forms.Form):
    email = forms.EmailField(required=True)
    captcha = CaptchaField(error_messages={"invalid": u"验证码错误"})


class ModifyPwdForm(forms.Form):
    password1 = forms.CharField(required=True, min_length=6)
    password2 = forms.CharField(required=True, min_length=6)

这里注意:

上面forms.py中的password1和password2必须和pwdreset.html中的password1和password2两个名字完全对应一致,

问题方可解决

发布了785 篇原创文章 · 获赞 357 · 访问量 166万+

猜你喜欢

转载自blog.csdn.net/appleyuchi/article/details/105038350
今日推荐