2010-06-18 50 views
1

在我的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抱怨?

+0

您的意思是您需要在登錄過程/授權中刪除用戶名? – 2010-06-19 18:16:12

回答

0

當我必須解決這個問題時,最簡單的方法是在註冊過程中不包含「擴展用戶配置文件」。當他們第一次登錄時,重定向他們或發送消息填寫表格。這應該至少讓你繼續下去。我很快就會解決這個問題,所以當我找到更具體的解決方案時,我會發布它。

我仍然不確定你的意思是無法訪問用戶名...這是auth.models.User的一部分,所以它是可用的。您是否忽略了用戶中已有的基本字段?...

+0

你可以建議如何覆蓋用戶名的字段,就像我可以做它的屬性是空白=真或我可以跳過它從不使用它?如果這麼好解釋的話。我擴展了django-auth模型,但不需要使用用戶名,但它在保存django-auth模型時給我錯誤。 – jahmed31 2017-09-29 13:31:05

0

爲什麼不根據first_name和last_name在保存時生成用戶名?

相關問題