就讓我們來看看在source爲兩類顯示:
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(r'^register/$',
RegistrationView.as_view(form_class=RegistrationFormTOSAndEmail),
name='registration_register')
我將如何傳遞'RegistrationFormTOSAndEmail'到Django的註冊提供的模板,而不是默認的? – user21398
我已經更新了答案。但通過搜索一次就可以輕鬆找到答案,無論是通過谷歌還是本網站。 – EWit
我認爲可能有兩件事情不在這裏:它應該是url(r ^/accounts/register/$'而不是url(r'^ accounts /'),並且一定要在該條目之前放置該行:url r'^ accounts /',include('registration.backends.default.urls')), – eezis