我想自定義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的登記表是由用戶輸入的用戶名替換?
究竟是你想怎麼辦?您是否只想在註冊時向用戶個人資料添加額外的信息? – 2012-02-17 06:56:13
是的,我想在註冊時添加額外的信息,並刪除該用戶名字段 – user1163236 2012-02-17 10:25:33