【发布时间】: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})
有什么想法吗?
【问题讨论】: