Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django如何使Django内置auth视图识别自定义表单_Python_Django_Django Authentication - Fatal编程技术网

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

Python Django如何使Django内置auth视图识别自定义表单,python,django,django-authentication,Python,Django,Django Authentication,我正在使用django内置的auth url和视图 所以,在url.py中 url(r'^accounts/', include('django.contrib.auth.urls')), in views.py from MyApp.forms import * from django.contrib.auth.views import * 我想自定义django inbuit viewpassword\u reset\u confirm函数使用的SetPasswordForm,因为我需要

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

所以,在url.py中

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

from MyApp.forms import *
from django.contrib.auth.views import *
我想自定义django inbuit view
password\u reset\u confirm
函数使用的SetPasswordForm,因为我需要对新密码进行更多检查1

在我的表格里 我有如下自定义的SetPasswordForm(SetPasswordForm已从
contrib.auth.forms导入)

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


非常感谢。

您应该重写URL以将自定义表单类作为view关键字参数传递。详细信息请参见。

您需要稍微修改一下URL

在您的
url.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')),
    ...
]
导入django.contrib.auth.views作为auth\u视图
从myapp.forms导入SetPasswordForm
URL模式=[
...
url(r'^accounts/reset/(?P[0-9A-Za-z_ \-])/(?P[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/”,包括('django.contrib.auth.url'),
...
]

重要的是,
password\u reset\u confirm
url高于其他url。

您需要手动将自定义表单作为关键字参数
set\u password\u form
传递到
password\u reset\u confirm
查看。您好。非常感谢您的回答,现在我已经理解了背后的逻辑并尝试了,但视图仍然不可用再次确认,您是否介意看一下后续问题和帮助?谢谢!@Mona密码的特定url\u reset\u确认url中需要更多内容,请参阅我的答案。