2012-02-17 175 views
3

我想自定義Django註冊表單。我有mysite項目,我在其中創建了registration1應用程序。自定義django註冊表

mysite的/ registration1/forms.py

from registration.forms import RegistrationForm 
from django import forms 
class UserRegistrationForm(RegistrationForm): 
    unique_id = forms.CharField(min_length=12,max_length=12,label=("Unique id")) 

mysite的/ regbackend.py

import profile 
from registration1.forms import UserRegistrationForm 
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 

class MyBackend(object): 

    def register(self, request, **kwargs): 
     username, email, password = kwargs['username'],kwargs['email'], kwargs['password1'] 
     if Site._meta.installed: 
     site = Site.objects.get_current() 
     else: 
      site = RequestSite(request) 
     new_user = RegistrationProfile.objects.create_inactive_user(username, email, 
                   password, site) 
     signals.user_registered.send(sender=self.__class__, 
           user=new_user, 
           request=request) 
     user = User.objects.get(username=username) 
     user.unique_id=kwargs['unique_id'] 
     address_user = Address() 
     address_user.save() 
     contact_user = Contact(address=address_user,email=user.email) 
     contact_user.save() 

     address_company = Address() 
     address_company.save() 
     contact_company = Contact(address=address_company) 
     contact_company.save() 
     company = Company(contact=contact_company, admin=user) 
     company.save() 
     user_profile = UserProfile(user=user,contact=contact_user,position='',company=company ) 
    user_profile.save() 
    user.save() 

     return new_user 

mysite的/ urls.py

from registration.views import register 
import registration.backends.default.urls as regUrls 
import mysite.regbackend 
from registration1.forms import UserRegistrationForm 
url(r'^accounts/register/$', register,{'backend':'mysite.regbackend.MyBackend','form_class': UserRegistrationForm},name='registration_register'), 

但是,這是給我的錯誤

'MyBackend' object has no attribute 'registration_allowed' 

我也想與UNIQUE_ID原Django的登記表是由用戶輸入的用戶名替換?

+0

究竟是你想怎麼辦?您是否只想在註冊時向用戶個人資料添加額外的信息? – 2012-02-17 06:56:13

+0

是的,我想在註冊時添加額外的信息,並刪除該用戶名字段 – user1163236 2012-02-17 10:25:33

回答

6

我建議你繼承和使用任何兩個默認後端覆蓋一些方法:

.... 
from registration.backends.default import SimpleBackend 
class MyBackend(SimpleBackend): 
    .... 

,或者如果你需要電子郵件確認後端:

.... 
from registration.backends.default import DefaultBackend 
class MyBackend(DefaultBackend): 
    .... 
+0

感謝您的回答真的有幫助 – user1163236 2012-03-08 17:43:19

+5

您認爲這可以解決您的問題嗎?你能在這種情況下標記嗎? – ramusus 2012-03-10 09:24:16