Django-registration:如何让账户创建要求输入名字和姓氏?

4
我正在使用Django-Registration包来让用户创建账户,进行身份验证并登录我的Web应用程序。
然而,账户创建的表单/视图并没有要求用户输入名字和姓氏(这些字段是模型的一部分)。它只要求他们输入电子邮件地址、登录ID和密码(两次)。我希望它能够要求用户输入名字和姓氏(这些字段可以是可选的...但仍应该询问)。我找不到要修改的表单和视图文件,以便要求用户提供这些信息。我已经修改了模板文件,但是没有表单和视图的修改是无用的。这该如何实现?
3个回答

6
在你的forms.py中,扩展DjangoRegistration表单的方式如下:
class MyExtendedForm(RegistrationForm):
    first_name = forms.CharField(widget=forms.TextInput(label="first_name"))
    last_name = forms.CharField(widget=forms.TextInput(label="last_name"))

urls.py 中,告诉 django-registration 使用这个扩展表单:
# using my registration form to override the default
(r'^register/$', 
    register, 
    {'backend': 'registration.backends.default.DefaultBackend',
     'form_class': MyExtendedForm}),

定义user_created来保存额外的信息:

def user_created(sender, user, request, **kwargs):
    """
    Called via signals when user registers. Creates different profiles and
    associations
    """
    form = MyExtendedForm(request.Post)
    # Update first and last name for user
    user.first_name=form.data['first_name']
    user.last_name=form.data['last_name']
    user.save()

然后,注册django-registration的信号以在任何注册处理完成后调用您的函数:

from registration.signals import user_registered
user_registered.connect(user_created)

在对您上面的代码进行了一些相对较小的编辑之后,它成功了。谢谢! - Saqib Ali
我的编辑仍未发布。没有它们,你的代码将无法工作。所以为了其他人的利益,我在这里发布它们:class MyExtendedForm中的两行代码都缺少一个右括号。另外,user_created中的第一行应该是form = MyExtendedForm(request.Post)。并且在你将其注册到Django-registration信号之前,必须先定义user_created函数。 - Saqib Ali
是的,我刚刚按照逻辑顺序提供了一个原型代码。很高兴它对你有用。 - zaphod
1
在使用django-registration 0.8时遇到了“无法导入名称register”的错误。我已经发布了一个修复方法 - Brian
有一个打字错误。应该是 request.POST(全部大写)。 - samir105

2

一旦您设置好了像这里所示的一样,使用以下CustomUserForm:

class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
        fields = ['first_name','last_name','username','email','password1','password2']

0

为了从默认的Django用户模型中添加字段first_namelast_name(提供您自己的表单类)

将这两个字段添加到默认RegistrationFormMeta.fields中:

from django_registration.forms import RegistrationForm

class RegistrationWithNameForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        fields = ["first_name", "last_name"] + RegistrationForm.Meta.fields

通过在urls.py中添加路径来覆盖默认的RegistrationView

from django_registration.backends.activation.views import RegistrationView
from yourapp.forms import RegistrationWithNameForm

path('accounts/register/',
  RegistrationView.as_view(form_class=RegistrationWithNameForm),
  name='django_registration_register',
),
path("accounts/", include("django_registration.backends.activation.urls")),

测试用例:

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

class RegistrationTestCase(TestCase):
    registration_url = reverse("django_registration_register")
    test_username = "testuser"
    post_data = {
        "first_name": "TestFirstName",
        "last_name": "TestLastName",
        "username": test_username,
        "email": "testuser@example.com",
        "password1": "mypass",
        "password2": "mypass"
    }

    def test_register(self):
        response = self.client.post(self.registration_url, self.post_data)
        self.assertRedirects(response, reverse("django_registration_complete"))
        user = get_user_model().objects.get(username=self.test_username)
        # Assert all fields are present on the newly registered user
        for field in ["username", "first_name", "last_name", "email"]:
            self.assertEqual(self.post_data[field], getattr(user, field))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接