【问题标题】:How to avoid creating 'username' in django-auth如何避免在 django-auth 中创建“用户名”
【发布时间】:2011-03-05 14:26:54
【问题描述】:

在我的 django 项目中,我需要添加注册功能。问题是在注册过程中我不能在任何地方使用“用户配置文件”。我的用户由 'first name' 、 'last name' 和其他一些数据定义。如何做到这一点?除了启用 contrib.auth 和“注册”之外,我还创建了一个“用户”应用程序。在 user.models 我有一个带有附加字段的扩展用户模型。在 user.forms 我创建了扩展注册表:

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 会抱怨?

【问题讨论】:

  • 你的意思是在登录过程/授权中需要去掉用户名吗?

标签: django django-registration django-authentication


【解决方案1】:

为什么不在保存时根据 first_name 和 last_name 生成用户名?

【讨论】:

    【解决方案2】:

    当我不得不解决这个问题时,最简单的方法是在注册过程中不包含“扩展用户配置文件”的内容。当他们第一次登录时,重定向他们或向他们发送消息以填写表格。这至少应该让你继续下去。我很快就会自己解决这个问题,所以当我找到更具体的解决方案时,我会发布它。

    我仍然不确定您无法访问用户名是什么意思...这是 auth.models.User 的一部分,所以它是可用的。您是否忽略了 User 中已有的基本字段?...

    【讨论】:

    • 您能否建议如何覆盖用户名字段,例如我可以将其属性设置为空白=True 或者我可以跳过它而不使用它吗???如果是这样,请解释一下。我正在扩展 django-auth 模型,但不需要使用用户名,但是在保存 django-auth 模型时它给了我错误。
    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多