无法将自定义字段添加到Django注册 [英] Not able to add custom fields to django-registration

查看:88
本文介绍了无法将自定义字段添加到Django注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了 RegistrationFormUniqueEmail

class CustomRegistrationFormUniqueEmail(RegistrationFormUniqueEmail):
    first_name = forms.CharField(label=_('First name'), max_length=30,required=True)
    last_name = forms.CharField(label=_('Last name'), max_length=30, required=True)
    def save(self, profile_callback=None):
        new_user = super(CustomRegistrationFormUniqueEmail, self).save(profile_callback=profile_callback)
        new_user.first_name = self.cleaned_data['first_name']
        new_user.last_name = self.cleaned_data['last_name']
        return new_user

然后更改视图

#       form = form_class(data=request.POST, files=request.FILES)
        form = CustomRegistrationFormUniqueEmail(data=request.POST, files=request.FILES)

但是仍然看到默认表单,其中仅包含四个字段。

But, still I see default form which contains four fields only.

推荐答案

我们最近实现了这种形式。我们要做的是:

We recently implemented such a form. Here's what we've done:


  • 创建新的后端(只需从默认后端复制它即可开始)

  • Create a new backend (just copy it from the default backend to start with)

registration/
    backends/
        default/
        custom/ # <- your new backend

...

在新的 urls.py 中调整后端参数

...
{ 'backend': 'registration.backends.custom.DefaultBackend' },
...


  • 自定义 forms.py c $ c>。根据您的喜好调整此表单(字段和验证)

  • Create a forms.py under custom. Adjust this form to your liking (fields and validations)

    registration / urls.py 点到适当的后端:

     # from registration.backends.default.urls import *
     from registration.backends.custom.urls import *
    


  • 这应该可行。尤其如此,因为:

    That should work. Particularly this works because:


    • 您的 custom / __ init __。py 将具有 DefaultBackend 类和 get_form_class 方法:

    def get_form_class(self, request):
        """
        Return the default form class used for user registration.
        """
        return RegistrationForm
    


  • 然后您导入自己的 RegistrationForm 也在该文件中:

    from registration.backends.custom.forms import RegistrationForm
    


  • 这篇关于无法将自定义字段添加到Django注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆