在我的django項目中,我需要添加註冊功能。問題在於,在註冊過程中,我無法在任何地方使用'userprofile'。我的用戶是由'名字','姓氏'和其他一些數據來定義的。如何實現這一目標?除了啓用contrib.auth和'註冊'我創建了一個'用戶'應用程序。在user.models中,我有一個擴展的用戶模型和其他字段。在user.forms我創建擴展登記表:如何避免在django-auth中創建'用戶名'
class ExtendedRegistrationForm(RegistrationForm):
first_name = forms.CharField(
label="First name",
error_messages={'required': 'Please fill the first name field'},
)
last_name = forms.CharField(
label="Last name",
error_messages={'required': 'Please fill the last name field'},
)
def save(self, profile_callback=None):
user = super(ExtendedRegistrationForm, self).save()
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
在user.views我有一個自定義註冊查看:
def custom_register(request, success_url=None,
form_class=ExtendedRegistrationForm, profile_callback=None,
template_name='registration/registration_form.html',
extra_context=None):
def _create_profile(user):
p = UserProfile(user=user)
p.is_active = False
p.first_name = first_name
p.last_name = last_name
p.save()
return register(request,
success_url="/accounts/register/complete",
form_class=ExtendedRegistrationForm,
profile_callback=_create_profile,
template_name='registration/registration_form.html',
extra_context=extra_context,
)
而且我已經覆蓋報名網址爲我的項目:
url(r'^accounts/password/reset/$',
auth_views.password_reset, { 'post_reset_redirect' : '/',
'email_template_name' : 'accounts/password_reset_email.html' },
name='auth_password_reset',),
url(r'^accounts/password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm, { 'post_reset_redirect' : '/accounts/login/'},
name='auth_password_reset_confirm'),
url(r'^accounts/password/reset/complete/$',
auth_views.password_reset_complete,
name='auth_password_reset_complete'),
url(r'^accounts/password/reset/done/$',
auth_views.password_reset_done,
name='auth_password_reset_done'),
url(r'^accounts/register/$',
'user.views.custom_register',
name='registration_register'),
(r'^accounts/', include('registration.urls')),
所以我有一個很好的基礎開始,但如何擺脫'用戶名'?我可以將用戶名作爲first_name(這麼多用戶具有相同名稱)或將Django抱怨?
您的意思是您需要在登錄過程/授權中刪除用戶名? – 2010-06-19 18:16:12