【问题标题】:Django: name 'SignupFormExtra' is not definedDjango:未定义名称“SignupFormExtra”
【发布时间】:2014-05-31 02:20:04
【问题描述】:

我目前安装了 django-userena,我正在尝试用自定义的注册表单覆盖默认注册表单,这样我就可以拥有额外的字段,但是作为 django 的新手,我碰巧被卡住了。遵循 userena 文档中的指南后,我有一个名为 accounts 的应用程序。在那个应用程序中,我创建了“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=True)

    last_name = forms.CharField(label=_(u'Last name'),
                                max_length=30,
                                required=True)
    industry = forms.CharField(label=_(u'Industry'),
                                max_length=50,
                                required=False)
    occupation = forms.CharField(label=_(u'Occupation'),
                                max_length=50,
                                required=False)
    bio = forms.TextField(label=_(u'Bio'),
                                required=True)
    skills = forms.TextField(label=_(u'Skills'),
                                required=False)
    interests = forms.TextField(label=_(u'Interests'),
                                max_length=50,
                                required=False)
    phone = forms.CharField(label=_(u'Phone'),
                                max_length=10,
                                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')
        new_order.insert(2, 'industry')
        new_order.insert(3, 'occupation')
        new_order.insert(4, 'bio')
        new_order.insert(5, 'skills')
        new_order.insert(6, 'interests')
        new_order.insert(7, 'phone')
        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()

        new_user.first_name = self.cleaned_data['first_name']
        new_user.last_name = self.cleaned_data['last_name']
        new_user.industry = self.cleaned_data['industry']
        new_user.occupation = self.cleaned_data['occupation']
        new_user.bio = self.cleaned_data['bio']      
        new_user.skills = self.cleaned_data['skills']
        new_user.interests = self.cleaned_data['interests']
        new_user.phone = self.cleaned_data['phone']  
        new_user.save()

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

然后将以下内容添加到我的项目 urls.py

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

但是,我的 urls 配置一定是搞砸了,因为我看到了一条有用的错误消息:

名称“SignupFormExtra”未定义

我的 urls.py 中的第 14 行是以下行:

{'signup_form': SignupFormExtra})

有什么想法吗?

【问题讨论】:

    标签: python django


    【解决方案1】:

    您需要将SignupFormExtra 导入您的urls.py

    在您的urls.py 顶部:

    from accounts.forms import SignupFormExtra
    

    确保您的forms.py 具有正确的相对路径。这假设您的urls.pyforms.py 在同一目录中。

    编辑:

    重新阅读您的问题,发现forms.py 位于名为accounts 的应用程序中。我调整了答案,以便您从 accounts.forms 导入。

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 2022-06-15
      • 2022-10-16
      • 2018-07-14
      • 2017-12-25
      • 2022-01-25
      • 2021-07-17
      • 2012-05-13
      相关资源
      最近更新 更多