我知道当我们需要制作django内置视图时,应该在内置视图函数可以使用它之前进行参数规范。
现在我想自定义django auth view password_reset_confirm
并在网址中导入我的自定义表单
from Myapp.forms import PasswordSetForm
from django.contrib.auth import urls,views
和网址
url(r'^accounts/ ^reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ ',
'django.contrib.auth.views.password_reset_confirm',{'set_password_form':PasswordSetForm},
name='password_reset_confirm'),
url(r'^accounts/', include('django.contrib.auth.urls')),
在我的form.py中,我导入了由django SetPasswordForm
funciton
password_reset_confirm
from django.contrib.auth.forms import SetPasswordForm
然后自定义表单
class PasswordSetForm(SetPasswordForm):
error_messages = {
'invalid_password': _("Please enter a valid password as instructed"),
'password_mismatch': _("The two password fields didn't match."),
}
#new_password1 = forms.CharField(label=_("New password"),
# widget=forms.PasswordInput)
#new_password2 = forms.CharField(label=_("New password confirmation"),
# widget=forms.PasswordInput)
new_password1 = forms.CharField(widget=forms.PasswordInput, min_length=6, label='New Password' )
new_password2 = forms.CharField(widget=forms.PasswordInput, min_length=6, label='Confirm new password')
def clean_new_password1(self):
new_password1 = self.cleaned_data.get("new_password1")
# password must contain both Digits and Alphabets
# password cannot contain other symbols
if new_password1.isdigit() or new_password1.isalpha() or not new_password1.isalnum():
raise forms.ValidationError(
self.error_messages['invalid_password'],
code='invalid_password',
)
return new_password1
您可以看到对new_password1进行了更多检查
但是在尝试多次之后页面仍然使用默认的SetpasswordForm,因为两个密码的默认标签显示在我的html而不是我的自定义标签中(在html {{form.new_password2.label}}
中用于显示标签)和没有对new_password1进行额外检查
我试图创建另一个不继承SetPassordForm的MyPasswordSetForm并将其传递给password_reset_confirm
,但它没有任何区别,仍然使用默认表单。
我已经google了很多并就此问了一些问题,这似乎是正确的方法,但这可能是什么问题?
非常感谢您的帮助。
答案 0 :(得分:2)
r'^accounts/ ^reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ '
应该是:
r'^accounts/reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$'