2010-06-18 31 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在保存时生成用户名?

相关问题