Python 如何为django注册创建自定义django后端?

Python 如何为django注册创建自定义django后端?,python,django,django-forms,django-registration,Python,Django,Django Forms,Django Registration,我已经读过了 我有机会制作自己的后端。我这样做是因为我想创建一个后端,不允许使用相同的电子邮件进行注册,我想更改电子邮件错误消息。我还想在我自己的领域添加 以下是我的想法: from django import forms from registration.forms import RegistrationForm from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models i

我已经读过了

我有机会制作自己的后端。我这样做是因为我想创建一个后端,不允许使用相同的电子邮件进行注册,我想更改电子邮件错误消息。我还想在我自己的领域添加

以下是我的想法:

from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from registration.forms import attrs_dict

class customRegistrationForm(RegistrationForm):
    email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
        maxlength=75)),
        label=_("Confirm email"))

    def clean_email(self):
        """
        Validate that the email is alphanumeric and is not already
        in use.
        """
        try:
            email = User.objects.get(email__iexact=self.cleaned_data['email'])
        except User.DoesNotExist:
            return self.cleaned_data['email']
        raise forms.ValidationError(_("That email already exists - if you have forgotten your password, go to the login screen and then select \"forgot password\""))

    def clean(self):
        """
        Verifiy that the values entered into the two email fields
        match. Note that an error here will end up in
        ``non_field_errors()`` because it doesn't apply to a single
        field.

        """
        if 'email' in self.cleaned_data and 'email2' in self.cleaned_data:
            if self.cleaned_data['email'] != self.cleaned_data['email2']:
                raise forms.ValidationError(_("The two email fields didn't match."))
        return super(RegistrationForm,clean)
上面的内容在我的init.py文件中(无论是什么文件)

然后,我的URL.py代码中有:

url(r'^accounts/register/$',
    register,
        { 'backend': 'myapp.forms.customRegistrationForm' },
    name='registration_register'),
... #other urls here!
现在,当我转到/accounts/register页面时,出现以下错误:

属性错误位于/帐户/注册表/

“customRegistrationForm”对象没有“允许注册”属性

这很奇怪。它似乎在告诉我,我需要一个“registration\u allowed”方法添加到我的子类中。但是,子类是RegistrationForm的一个子类,它运行良好,并且没有定义这些内容。。。我知道我可以加入这些成员,但这似乎超出了扩展的目的,对吗

更新

这是代码,现在它的工作

我将不同的类划分为不同文件夹中的不同的init.py文件-一个称为“forms”,一个称为“backends”,它们都位于我的主项目下的文件夹“djangRegistration”中

/表单/init.py

from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from registration.forms import attrs_dict

class customRegistrationForm(RegistrationForm):
    def __init__(self, *args, **kw):
        super(RegistrationForm, self).__init__(*args, **kw)
        self.fields.keyOrder = [
            'username',
            'email',
            'email2',
            'password1',
            'password2'
        ]

    email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
        maxlength=75)),
        label=_("Confirm email"))

    def clean_email(self):
        """
        Validate that the email is alphanumeric and is not already
        in use.
        """
        try:
            email = User.objects.get(email__iexact=self.cleaned_data['email'])
        except User.DoesNotExist:
            return self.cleaned_data['email']
        raise forms.ValidationError(_("That email already exists - if you have forgotten your password, go to the login screen and then select \"forgot password\""))

    def clean(self):
        """
        Verifiy that the values entered into the two email fields
        match. Note that an error here will end up in
        ``non_field_errors()`` because it doesn't apply to a single
        field.

        """
        if 'email' in self.cleaned_data and 'email2' in self.cleaned_data:
            if self.cleaned_data['email'] != self.cleaned_data['email2']:
                raise forms.ValidationError(_("The two email fields didn't match."))
        return super(RegistrationForm,clean)
from registration.backends.default import DefaultBackend
from dumpstownapp.djangoRegistration.forms import customRegistrationForm

class customDefaultBackend(DefaultBackend):
    def get_form_class(self, request):
        """
        Return the default form class used for user registration.

        """
        return customRegistrationForm
/后端/init.py

from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from registration.forms import attrs_dict

class customRegistrationForm(RegistrationForm):
    def __init__(self, *args, **kw):
        super(RegistrationForm, self).__init__(*args, **kw)
        self.fields.keyOrder = [
            'username',
            'email',
            'email2',
            'password1',
            'password2'
        ]

    email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
        maxlength=75)),
        label=_("Confirm email"))

    def clean_email(self):
        """
        Validate that the email is alphanumeric and is not already
        in use.
        """
        try:
            email = User.objects.get(email__iexact=self.cleaned_data['email'])
        except User.DoesNotExist:
            return self.cleaned_data['email']
        raise forms.ValidationError(_("That email already exists - if you have forgotten your password, go to the login screen and then select \"forgot password\""))

    def clean(self):
        """
        Verifiy that the values entered into the two email fields
        match. Note that an error here will end up in
        ``non_field_errors()`` because it doesn't apply to a single
        field.

        """
        if 'email' in self.cleaned_data and 'email2' in self.cleaned_data:
            if self.cleaned_data['email'] != self.cleaned_data['email2']:
                raise forms.ValidationError(_("The two email fields didn't match."))
        return super(RegistrationForm,clean)
from registration.backends.default import DefaultBackend
from dumpstownapp.djangoRegistration.forms import customRegistrationForm

class customDefaultBackend(DefaultBackend):
    def get_form_class(self, request):
        """
        Return the default form class used for user registration.

        """
        return customRegistrationForm
最后,my urls.py只引用了新的后端:

url(r'^accounts/register/$',
    register,
        { 'backend': 'myapp.djangoRegistration.backends.customDefaultBackend' },
    name='registration_register'),
#more urls here! yay!
最后,我必须添加一些代码来“排序”字段的显示方式,这就是customRegistrationForm中的init方法所做的


谢谢

您试图使用表单作为后端,但这根本不是后端。正如您链接到的文档所解释的,后端是实现某些方法的类,包括
registration\u allowed
。表单没有实现任何这些,这并不奇怪,因为它是用于用户输入和验证的,而不是后端操作


然而,该页面确实给出了一个关于实现此功能的正确方法的提示。后端可以定义的方法之一是
get\u form\u class()
,它返回要使用的表单类。因此,您需要的似乎是一个自定义后端,它继承自
registration.backends.default.DefaultBackend
,只覆盖
get\u form\u类
方法,该方法只返回
customRegistrationForm

哈哈哈!谢谢,太棒了。让我试试看!(当我读到你的答案时,我只是发出了一个大大的“Ohhhhhhhh”)