userena-django:未显示注册表单的额外字段

时间:2016-11-19 21:34:53

标签: django django-forms

我使用django-userena作为项目,我需要在注册表单中添加额外的字段。添加字段时,它们不会显示在表单上。 创建一个名为forms.py的新文件,并使用文档中给出的示例:

forms.py

from django import forms
from django.utils.translation import ugettext_lazy as _

from userena.forms import SignupForm

class SignupFormExtra(SignupForm):
    """
    A form to demonstrate how to add extra fields to the signup form, in this
   case adding the first and last name.


   """
   first_name = forms.CharField(label=_(u'First name'),
                             max_length=30,
                             required=False)

   last_name = forms.CharField(label=_(u'Last name'),
                            max_length=30,
                            required=False)

   def __init__(self, *args, **kw):
       """

       A bit of hackery to get the first name and last name at the top of the
       form instead at the end.

       """
        super(SignupFormExtra, self).__init__(*args, **kw)
        # Put the first and last name at the top
        new_order = self.fields.keyOrder[:-2]
        new_order.insert(0, 'first_name')
        new_order.insert(1, 'last_name')
        self.fields.keyOrder = new_order

   def save(self):
        """
        Override the save method to save the first and last name to the user
        field.

        """
        # First save the parent form and get the user.
        new_user = super(SignupFormExtra, self).save()

        # Get the profile, the `save` method above creates a profile for each
        # user because it calls the manager method `create_user`.
        # See: https://github.com/bread-and-pepper/django-    userena/blob/master/userena/managers.py#L65
        user_profile = new_user.get_profile()

        user_profile.first_name = self.cleaned_data['first_name']
        user_profile.last_name = self.cleaned_data['last_name']
        user_profile.save()

        # Userena expects to get the new user from this form, so return the new
        # user.
        return new_user

恩urls.py

from django.conf.urls import url,include
from django.contrib import admin
from apps.accounts.forms import SignupFormExtra

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('userena.urls')),
    url(r'^accounts/signup/$','userena.views.signup',{'signup_form':     SignupFormExtra}),
]

但它不起作用,表格保持不变

introducir la descripción de la imagen aquí

有人知道解决方案吗?谢谢!

0 个答案:

没有答案