我希望用户同意服务条款并支持独特的电子邮件。django-registration 有两个不同的子类注册表来执行此操作: RegistrationFormTermsOfServiceRegistrationFormUniqueEmail.

我是否必须制作自己的 RegistrationForm 子类,然后提供这两个功能?如果是的话,这将如何实现?注册表单会存在于我的应用程序的 forms.py 中还是其他地方?

有帮助吗?

解决方案

快速浏览一下 来源 对于两个类的显示:

class RegistrationFormTermsOfService(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which adds a required checkbox
    for agreeing to a site's Terms of Service.

    """
    tos = forms.BooleanField(widget=forms.CheckboxInput,
                             label=_(u'I have read and agree to the Terms of Service'),
                             error_messages={'required': _("You must agree to the terms to register")})


class RegistrationFormUniqueEmail(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which enforces uniqueness of
    email addresses.

    """
    def clean_email(self):
        """
        Validate that the supplied email address is unique for the
        site.

        """
        if User.objects.filter(email__iexact=self.cleaned_data['email']):
            raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
        return self.cleaned_data['email']

正如您所看到的,这两个类不会覆盖另一个类定义的方法,因此您应该能够将自己的类定义为:

from registration.forms import RegistrationFormUniqueEmail, RegistrationFormTermsOfService
class RegistrationFormTOSAndEmail(RegistrationFormUniqueEmail, RegistrationFormTermsOfService):
    pass

它应该可以运行,但是我还没有对此进行测试。至于在哪里开设这门课; forms.py 是一个很好的位置。

更新:

一点点阅读 https://django-registration.readthedocs.org/en/latest/views.html 这告诉我们可以通过 url 定义向视图传递一些参数;例如表单类。只需使用如下 URL:

url(r'^register/$',
    RegistrationView.as_view(form_class=RegistrationFormTOSAndEmail), 
    name='registration_register')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top