无法将自定义字段添加到 django-registration
我扩展了 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)
但是,我仍然看到仅包含四个字段的默认表单。
I extended 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
then changed view
# 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们最近实施了这样一个表格。这是我们所做的:
创建一个新的
后端
(只需从默认后端复制它即可开始)...
在新的
urls.py 调整后端参数
<前><代码>...
{ 'backend': 'registration.backends.custom.DefaultBackend' },
...
在
custom
下创建一个forms.py
。根据您的喜好调整此表单(字段和验证)在
registration/urls.py
中指向正确的后端:这应该可以工作。特别是这是有效的,因为:
您的
custom/__init__.py
将有一个带有get_form_class
方法的DefaultBackend
类:而您也在该文件中导入您自己的
RegistrationForm
: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)...
In the new
urls.py
adjust the backend argumentsCreate a
forms.py
undercustom
. Adjust this form to your liking (fields and validations)In the
registration/urls.py
point to the proper backend:That should work. Particularly this works because:
Your
custom/__init__.py
will have aDefaultBackend
class with aget_form_class
method:And you import your own
RegistrationForm
in that file, too:我不确定,为什么它不起作用,但我很确定你不需要编辑 django-registration 的 views.py ......你可以将新的
CustomRegistrationFormUniqueEmail
作为urls.py
中的参数传递。I'm not sure, off hand, why it isn't working but I am pretty sure you do not need to edit django-registration's
views.py
... you can pass your newCustomRegistrationFormUniqueEmail
as an argument inurls.py
.您可以尝试查看此处 使用信号扩展 django-registration 和此处 http://dmitko.ru/?p=546
You can try to look here Extending django-registration using signals and here http://dmitko.ru/?p=546