为什么django内置认证意见不能识别定制表单? [英] why django inbuilt auth views not recognizing the customized form?

查看:170
本文介绍了为什么django内置认证意见不能识别定制表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白,当我们需要使用django内置的视图时,参数规范应该在内置视图函数使用之前进行。

I understand that when we need to make the django inbuilt views, parameter specification should be made before the inbuilt view function could use it.

现在我要自定义django auth视图的形式 password_reset_confirm

now I want to customize the form for django auth viewpassword_reset_confirm

在url中,我导入我的自定义表单

and in the url, I import my customized form

from Myapp.forms import PasswordSetForm
from django.contrib.auth import urls,views

和url

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')),

$ b我的form.py中的
$ b

我将导入用作默认表单的原始 SetPasswordForm django password_reset_confirm funciton

in my form.py i import the original SetPasswordForm that is used as default form by django password_reset_confirm funciton

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

as you could see more checking are done for the new_password1

完成的。但是,在尝试了很多次之后,该页面仍然使用默认的SetpasswordForm作为默认标签在我的html中显示两个密码,而不是我的自定义标签(在html {{form.new_password2.label}}中用于显示标签),并且不会对new_password1完成

But after trying many times the page is still using the default SetpasswordForm as the default labels for the two password is displayed in my html instead of my customized label(in the html {{form.new_password2.label}} is used to display label) and no additional checking on the new_password1 is done

我已经尝试创建另一个MyPasswordSetForm,它不会继承SetPassordForm并将其传递给 password_reset_confirm ,但没有任何区别,仍然使用默认表单。

I have tried to create another MyPasswordSetForm that does not inherit the SetPassordForm and pass this to the password_reset_confirm but it does not make any difference, the default form is still used.

我已经google了很多,并提出了这个问题,似乎这是正确的方法,但这里可能是什么问题?

I have googled lot and ask questions on this, seems this is the right way to go, but What could be the problem here?

非常感谢您的帮助。

推荐答案

URL regexp看起来错误错误:

Oops, that URL regexp looks wrong wrong wrong:

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})/$'

这篇关于为什么django内置认证意见不能识别定制表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆