向django注册表单添加额外的字段 [英] Adding extra fields to django-registration form
问题描述
我有一个名为组织的模型,我已经设置为用户配置文件,我想让组织模型中的字段显示在注册页面上。如何使用django注册进行此操作。
I have a model called "Organization" that I've setup as a User profile and I would like to have the fields from the "Organization" model show up on the registration page. How do I go about doing this with django-registration.
# models.py
class Organization(models.Model):
user = models.ForeignKey(User, unique=True)
logo = models.ImageField(upload_to='organizations')
name = models.CharField(max_length=100, null=True, unique=True)
# more fields below etc.
# settings.py
AUTH_PROFILE_MODULE = 'volunteering.organization'
推荐答案
最简单的方法是在 django注册
0.8]:
The easiest way to do this would be [tested on django-registration
0.8]:
在项目的某个地方,在组织应用中说forms.py
from registration.forms import RegistrationForm
from django.forms import ModelForm
from models import Organization
class OrganizationForm(forms.ModelForm):
class Meta:
model = Organization
RegistrationForm.base_fields.update(OrganizationForm.base_fields)
class CustomRegistrationForm(RegistrationForm):
def save(self, profile_callback=None):
user = super(CustomRegistrationForm, self).save(profile_callback=None)
org, c = Organization.objects.get_or_create(user=user, \
logo=self.cleaned_data['logo'], \
name=self.cleaned_data['name'])
然后在你的根urlconf中[但是在正则表达式上面包括 registration.urls
,并假设正则表达式是 r'^ accounts /'
] add:
Then in your root urlconf [but above the regex pattern that includes registration.urls
and assuming that regex is r'^accounts/'
] add:
from organization.forms import CustomRegistrationForm
urlpatterns += patterns('',
(r'^accounts/register/$', 'registration.views.register', {'form_class':CustomRegistrationForm}),
)
显然,你也可以创建自定义后端,但是IMHO这样比较容易。
Obviously, you can also create a custom backend, but IMHO this is way easier.
这篇关于向django注册表单添加额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!