Django如何使Django内置auth视图识别自定义表单

2024-10-06 12:26:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用django内置的auth url和视图

所以,在网址.py在

url(r'^accounts/', include('django.contrib.auth.urls')),

在视图.py在

^{pr2}$

我想定制django inbuit viewpassword_reset_confirm函数使用的SetPasswordForm,因为我需要对新的密码1进行更多检查

在我的表单.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参数(以指定要使用的表单)对吗?如果有任何错误,请纠正我

非常感谢。在


Tags: djangopyselfauth视图url表单new
2条回答

您应该重写URL以将自定义窗体类作为视图关键字参数传递。How to override a view from an external Django app中的详细信息。在

你需要修改一下你的网址。在

在你的urls.py中,这个。。。在

urlpatterns = [
    ...
    url(r'^accounts/', include('django.contrib.auth.urls')),
    ...
]

需要。。。在

^{pr2}$

password_reset_confirmurl必须位于另一个url之上。在

相关问题 更多 >