0

RegistrationForm在我自己的应用程序中继承了 django-registration 的基本内容,所以有:

表格.py

from registration.forms import RegistrationForm

class CustomRegistrationForm(RegistrationForm):
"""
Form for registering a new user account.

Validates that the requested username is not already in use, and
requires the password to be entered twice to catch typos.

Subclasses should feel free to add any additional validation they
need, but should avoid defining a ``save()`` method -- the actual
saving of collected user data is delegated to the active
registration backend.

"""

print 'Custom Form'

def clean_password1(self):
    """
    Verify that password is longer than 5 characters.

    """

    password = self.cleaned_data['password1']
    print 'Custom Validator'
    if len(password) < 6:
        raise forms.ValidationError(_("Password needs to be at least 6 characters long"))
    return self.cleaned_data

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']

网址.py

    url(r'^register/$', 'registration.views.register',
{'form_class':CustomRegistrationForm,
    'backend':'registration.backends.default.DefaultBackend' }, name='registration_register'),

...应该可以添加我的验证。当我加载表单时,Custom form会打印到控制台,但Custom validator不会。如果我将virtualenv 站点包中的 django-registration 包中print 'Custom validator'的自定义验证器放在同一个位置,它会在调用表单时打印到控制台。RegistrationForm

当然,问题是自定义验证在我自己的应用程序中被子类化时不起作用。出于某种原因,我的自定义表单正在运行,但验证器没有运行,即使它们是相同的。

我尝试了不同的 def 名称,例如clean(self)and clean_password(self)。我已经尝试了很多很多其他的修复程序,这些修复程序让我陷入了困境(例如,我自己的注册版本通过 pip 在 heroku 上安装,有自己的一批问题)。不过,这种子类化是最干净的方式,我觉得它应该可以工作。

为什么会发生这种情况?

4

1 回答 1

1

clean_password1看起来不错,除了你应该password在最后返回,而不是自定义验证器。

请注意,django 注册 1.0 现已发布。它已被重写以使用基于类的视图。尝试升级,您可能会在此过程中解决您的问题。

于 2013-06-23T21:21:42.263 回答