我在自己的应用程序中继承了django-registration的基本RegistrationForm
,所以:
forms.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']
urls.py
url(r'^register/$', 'registration.views.register',
{'form_class':CustomRegistrationForm,
'backend':'registration.backends.default.DefaultBackend' }, name='registration_register'),
...应该可以添加我的验证。加载表单时,Custom form
会打印到控制台,但Custom validator
不会。如果我将print 'Custom validator'
放在virtualenv site-packages的django-registration包中的RegistrationForm
中的自定义验证器中的相同位置,它会在调用表单时打印到控制台。
当然,问题是自定义验证在我自己的应用程序中被子类化时不起作用。由于某种原因,我的自定义表单正在运行,但验证器不是,即使它们是相同的。
我尝试了不同的def名称,例如clean(self)
和clean_password(self)
。我已经尝试了许多其他修复程序,这些修复程序已经让我远离兔子洞(例如,通过pip on heroku,its own batch of problems)分配我自己的注册版本。这个子类化是最干净的方式,我觉得应该有效。
为什么会发生这种情况?
答案 0 :(得分:1)
您的clean_password1
看起来没问题,但您最后应该返回password
,而不是自定义验证器。
请注意,django registration 1.0现已发布。它已被重写为使用基于类的视图。尝试升级,您可以在此过程中解决问题。