django中的form.is_valid()总是返回False
生活随笔
收集整理的這篇文章主要介紹了
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兩個名字完全對應一致,
問題方可解決
總結
以上是生活随笔為你收集整理的django中的form.is_valid()总是返回False的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Make sure you've inc
- 下一篇: authenticate总是返回None