2011-06-20 52 views
6

Django註冊在forms.py文件中有幾個表單類。一個是「類RegistrationFormTermsOfService(RegistrationForm)..如何在Django註冊中使用不同的表格

有什麼值得我的Django的註冊代碼的其餘部分更改,使這種形式在我的註冊流程,而不是RegistrationForm?

回答

5

你可以簡單地進入你的urls.py並做覆蓋表單類例如:

from registration.forms import RegistrationFormTermsOfService 

(r'^accounts/register/$', 'registration.views.register', {'form_class' : RegistrationFormTermsOfService}), 
+0

中的'後端'設置,這聽起來非常簡單...並且新表單應該在重新裝載reg表單時顯示? – Brenden

+0

爲什麼你不試試:-)(提示:是的,它應該) –

+0

所以我把這個放在我的主urls.py中,並且在註冊行之前。但我得到的錯誤寄存器()至少需要2個非關鍵字參數(給出1) – Brenden

1

你需要寫一個新的註冊如果你只是擴展新的字段,你可以繼承現有的認證表單,然後你需要編寫一個新的後端來處理表單,最後你需要編寫你自己的url, auth_urls並重新定義url以通過更改傳遞給視圖的變量切換視圖中的後端和身份驗證表單。

這對brea k開放源代碼以查看事情是如何工作的。我的結構基於原始的django註冊代碼,以保持一致。

+0

此表單已經存在於forms.py文件中。我是否還需要寫一個新的註冊表單?我真的想要使用其他類(在問題中引用)而不是標準的Reg類。只是不知道如何讓那一個成爲活動的形式。 – Brenden

5

這是一個使用自定義表單和後臺的實際示例,它設置了用戶名==電子郵件地址,並且僅在註冊時提示用戶輸入電子郵件地址。在,例如, my_registration.py

from django.conf import settings 
from django.contrib.sites.models import RequestSite 
from django.contrib.sites.models import Site 

from registration import signals 
from registration.forms import RegistrationForm 
from registration.models import RegistrationProfile 
from registration.backends.default import DefaultBackend 

class EmailRegistrationForm(RegistrationForm): 
    def __init__(self, *args, **kwargs): 
     super(EmailRegistrationForm,self).__init__(*args, **kwargs) 
     del self.fields['username'] 

    def clean(self): 
     cleaned_data = super(EmailRegistrationForm,self).clean() 
     if 'email' in self.cleaned_data: 
      cleaned_data['username'] = self.cleaned_data['username'] = self.cleaned_data['email'] 
     return cleaned_data 


class EmailBackend(DefaultBackend): 
    def get_form_class(self, request): 
     return EmailRegistrationForm 

my_registration_urls.py

在你的核心 urls.py
from django.conf.urls.defaults import * 
from django.views.generic.simple import direct_to_template 

from registration.views import activate 
from registration.views import register 

urlpatterns = patterns('', 
        url(r'^activate/complete/$', 
         direct_to_template, 
         { 'template': 'registration/activation_complete.html' }, 
         name='registration_activation_complete'), 
        # Activation keys get matched by \w+ instead of the more specific 
        # [a-fA-F0-9]{40} because a bad activation key should still get to the view; 
        # that way it can return a sensible "invalid key" message instead of a 
        # confusing 404. 
        url(r'^activate/(?P<activation_key>\w+)/$', 
         activate, 
         { 'backend': 'my_registration.EmailBackend' }, 
         name='registration_activate'), 
        url(r'^register/$', 
         register, 
         { 'backend': 'my_registration.EmailBackend' }, 
         name='registration_register'), 
        url(r'^register/complete/$', 
         direct_to_template, 
         { 'template': 'registration/registration_complete.html' }, 
         name='registration_complete'), 
        url(r'^register/closed/$', 
         direct_to_template, 
         { 'template': 'registration/registration_closed.html' }, 
         name='registration_disallowed'), 
        (r'', include('registration.auth_urls')), 
        ) 

然後,請確保您有:

url(r'^accounts/', include('my_registration_urls')), 
+0

請注意my_registration_urls.py ... – Darb

13

更新接受的答案使用Django 1.5和最新版本的Django登記的符合:

在urls.py

from registration.forms import RegistrationFormTermsOfService 
from registration.backends.default.views import RegistrationView 

urlpatterns = patterns('', 
    url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationFormTermsOfService), name='registration_register'), 
    # your other URLconf stuff follows ... 
) 

然後更新registration_form.html模板,並添加一個tos場,例如:

<p> 
<label for="id_tos">I accept the terms of service</label> 
{% if form.tos.errors %} 
    <p class="errors">{{ form.tos.errors.as_text }}</p> 
{% endif %} 
{{ form.tos }} 
</p> 
+0

if你想使用簡單的工作流程,它不需要電子郵件激活,並且用戶立即被激活,導入這個視圖,而不是'from.registration.backends.simple.views import RegistrationView'。 – static