我正在使用django内置的auth网址和视图
所以,在url.py中
url(r'^accounts/', include('django.contrib.auth.urls')),
在views.py中
from MyApp.forms import *
from django.contrib.auth.views import *
我想自定义django inbuit view password_reset_confirm
函数使用的SetPasswordForm,因为我需要使用new_password1进行更多检查
在我的forms.py中,我有自定义的SetPasswordForm,如下所示(使用从contrib.auth.forms already
导入的SetPasswordForm
class SetPasswordForm(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(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):
password1 = self.cleaned_data.get("password1")
# password must contain both Digits and Alphabets
# password cannot contain other symbols
if password1.isdigit() or password1.isalpha() or not password1.isalnum():
raise forms.ValidationError(
self.error_messages['invalid_password'],
code='invalid_password',
)
return password1
有谁可以帮助解释为什么django内置视图无法识别我的自定义SetPasswordForm以及是否可以这样做?
如果它不是要识别自定义的SetPasswordForm,那就意味着我必须重新定义url和views参数(以指定要使用的表单)我是对的吗?如果有任何错误,请纠正我
非常感谢。
答案 0 :(得分:1)
您应该覆盖URL以将自定义表单类作为view关键字参数传递。详情请见How to override a view from an external Django app。
答案 1 :(得分:1)
您需要稍微修改一下您的网址。
在urls.py
,这......
urlpatterns = [
...
url(r'^accounts/', include('django.contrib.auth.urls')),
...
]
需要...
import django.contrib.auth.views as auth_views
from myapp.forms import SetPasswordForm
urlpatterns = [
...
url(r'^accounts/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
auth_views.password_reset_confirm, {'set_password_form': SetPasswordForm}, name='password_reset_confirm'),
url(r'^accounts/', include('django.contrib.auth.urls')),
...
]
password_reset_confirm
网址高于其他网址至关重要。