2014-01-08 88 views
1

我希望用戶同意TOS並支持獨特的電子郵件。 django註冊有兩種不同的子註冊註冊表格:RegistrationFormTermsOfServiceRegistrationFormUniqueEmail如何在django註冊中註冊註冊表格

我是否必須製作自己的RegistrationForm sublcass,然後提供這些功能?如果是這樣,這將如何完成?註冊表單是否會存在於我的應用程序的forms.py或其他地方?

回答

1

就讓我們來看看在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') 
+0

我將如何傳遞'RegistrationFormTOSAndEmail'到Django的註冊提供的模板,而不是默認的? – user21398

+0

我已經更新了答案。但通過搜索一次就可以輕鬆找到答案,無論是通過谷歌還是本網站。 – EWit

+0

我認爲可能有兩件事情不在這裏:它應該是url(r ^/accounts/register/$'而不是url(r'^ accounts /'),並且一定要在該條目之前放置該行:url r'^ accounts /',include('registration.backends.default.urls')), – eezis