问题
I am currently using django-registration v0.8a and django-recaptcha for my registration portion. Everything is working fine with the recaptcha field showing up except that I am unable to get the RegistrationFormUniqueEmail to work. Here are some of the details.
I have ensured that my captcha\forms.py is indeed subclassing from the correct form:
from registration.forms import RegistrationFormUniqueEmail
class RegistrationFormCaptcha(RegistrationFormUniqueEmail):
captcha = ReCaptchaField(attrs={'theme': 'white'})
I have also placed the form_class key in all the urls associated with the register view which handles the call, for instance:
url(r'^register/$',
register,
{ 'form_class': RegistrationFormUniqueEmail,
'backend': 'registration.backends.default.DefaultBackend' },
name='registration_register'),
One strange behaviour I have noticed is that when I attempt to change the labels on my forms, the changes are not being reflected. Perhaps this is part of the same problem as I might have overlooked something?
class RegistrationForm(forms.Form):
"""
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.
"""
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_("Username"),
error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_("Email address"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password (again)"))
i.e I change one of the labels to another phrase, shouldn't that be reflected?
Thanks for viewing!
回答1:
The solution I'm using is to create a form using RegistrationFormCaptcha and RegistrationFormUniqueEmail and use it with the captcha backend in urls.
custom_registration/forms.py
from captcha.forms import RegistrationFormCaptcha
from registration.forms import RegistrationFormUniqueEmail
class RegistrationFormUniqueEmailRecaptcha(RegistrationFormUniqueEmail, RegistrationFormCaptcha):
pass
urls.py
from custom_registration.forms import RegistrationFormUniqueEmailRecaptcha
...
url(r'^w/accounts/register/$',
'registration.views.register',
{'backend': 'captcha.backends.default.DefaultBackend',
'form_class': RegistrationFormUniqueEmailRecaptcha}),
....
来源:https://stackoverflow.com/questions/8909819/django-registration-form-and-registration-unique-email-form