【问题标题】:Django-Registration: Account creation that asks for First Name/Last Name?Django-Registration:创建要求名字/姓氏的帐户?
【发布时间】:2013-02-02 19:07:39
【问题描述】:

这个问题是 Muki 在此处解释的解决方案的后续问题:

Problem in adding custom fields to django-registration

我已经安装并成功使用了 Django-registration 包。默认情况下,当您使用此软件包创建帐户时,它会询问您的用户名、电子邮件地址和密码。我希望它还要求(可选)名字+姓氏。 Muki 在上面链接中的回答解释了如何做到这一点。

但是,Muki 忽略了他在 custom/forms.py 中创建的文件中应该包含的内容。我需要知道我应该在这里创建的类的名称是什么以及字段定义应该是什么样的。

有人可以发布一个示例 forms.py,我可以用它来完成我正在尝试做的事情吗?

【问题讨论】:

    标签: django


    【解决方案1】:

    如果您的自定义后端位于 custom 文件夹中,那么 custom/forms.py 可能是这样的:

    from django import forms
    from registration.forms import RegistrationForm
    
    
    class RegistrationFormWithName(RegistrationForm):
        first_name = forms.CharField(required=False)
        last_name = forms.CharField(required=False)
    

    这会将您的两个可选字段添加到默认注册表单中。要告诉您的自定义后端使用这个新表单而不是默认表单,您需要更改 custom/__init__.py 中的 get_form_class 方法 Muki's answer 解释了如何做到这一点。

    当然,您还需要处理保存来自first_namelast_name 字段的数据。

    编辑

    您的custom/__init__.py 需要如下所示:

    from registration.backends.default import DefaultBackend
    from registration.backends.custom.forms import RegistrationFormWithName
    
    class CustomBackend(DefaultBackend):
        def get_form_class(self, request):
            return RegistrationFormWithName
    

    custom/urls.py 需要这样一行:

    url(r'^register/$', register, {'backend': 'registration.backends.custom.CustomBackend'}, name='registration_register'),
    

    将两个文件中的CustomBackend 类的名称更改为您所称的新后端。

    查看default backend source 以了解您可以覆盖的其他方法。希望对您有所帮助。

    【讨论】:

    • 我迷路了。仍然看到只有 4 个字段的注册表单 custom.forms.py: from django import forms from registration.forms import RegistrationForm class RegistrationFormWithName(RegistrationForm): first_name=forms.CharField(required=False) last_name=forms.CharField(required=False) custom/__init.py__ 包括:从registration.forms 导入RegistrationForm 从registration.backends.custom.forms 导入RegistrationForm custom/urls.py,我改变了这样的行:url(r'^register/$', register, {'backend ': 'registration.backends.custom.DefaultBackend'}, name='registration_register'),
    【解决方案2】:

    link 这个link 很好地解释了这个过程并与 django-registration 1.0 一起使用

    除了上面的代码之外,这里还有一些额外的指针。

    要更新名字,请在 models.py 中更改此项

    def user_registered_callback(sender, user, request, **kwargs):
    profile = ExUserProfile(user = user)
    profile.is_human = bool(request.POST["is_human"])
    user.first_name = request.POST["firstname"]
    user.save()
    profile.save()
    
    user_registered.connect(user_registered_callback)
    

    在forms.py文件中

    class ExRegistrationForm(RegistrationForm):
        is_human = forms.BooleanField(label = "Are you human?:")
        firstname = forms.CharField(max_length=30)
        lastname = forms.CharField(max_length=30)
    

    最后看到表格上的变化创建一个合适的模板。通过在您的应用程序中创建一个名为 admin.py 的文件并编写以下代码,可以在管理员中看到该配置文件

    from django.contrib import admin
    from django.contrib.auth.models import User
    from django.contrib.auth.admin import UserAdmin
    from prof.models import ExUserProfile
    
    admin.site.unregister(User)
    
    class UserProfileInline(admin.StackedInline):
        model = ExUserProfile
    
    class UserProfileAdmin(UserAdmin):
        inlines = [ UserProfileInline, ]
    
    admin.site.register(User, UserProfileAdmin)
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 2013-05-01
      • 2013-06-13
      • 2023-04-03
      • 2016-02-22
      • 2019-01-06
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多