設置
您使用的是django-profiles和django-registration項目?如果不是,你應該—這段代碼中的大部分已經爲你編寫了。
檔案
您的用戶配置文件的代碼是:
class Profile(models.Model):
user = models.ForeignKey(User, unique=True)
born = models.DateTimeField('born to')
photo = models.ImageField(upload_to='profile_photo')
你有沒有正確設置此配置文件在你的Django設置?如果你不應該添加此,爲你的應用程序的名稱替換yourapp
:
AUTH_PROFILE_MODULE = "yourapp.Profile"
登記表
django-registration
帶有一些默認的登記表格,但你指定你想創建自己的。每個Django form field defaults to required所以你不應該改變這一點。重要的部分就是確保處理現有的註冊表單字段並添加配置文件創建。像這樣的東西應該工作:
from django import forms
from registration.forms import RegistrationForm
from yourapp.models import Profile
from registration.models import RegistrationProfile
class YourRegistrationForm(RegistrationForm):
born = forms.DateTimeField()
photo = forms.ImageField()
def save(self, profile_callback=None):
new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
password=self.cleaned_data['password1'],
email = self.cleaned_data['email'])
new_profile = Profile(user=new_user, born=self.cleaned_data['born'], photo=self.cleaned_data['photo'])
new_profile.save()
return new_user
把它一起
您可以使用默認django-registration
模板和意見,但希望通過他們的形式urls.py
:
from registration.backends.default import DefaultBackend
from registration.views import activate
from registration.views import register
# ... the rest of your urls until you find somewhere you want to add ...
url(r'^register/$', register,
{'form_class' : YourRegistrationForm, 'backend': DefaultBackend},
name='registration_register'),
謝謝你非常 – xRobot 2010-01-06 15:17:08