向 django-registration 表单添加额外的字段 [英] Adding extra fields to django-registration form

查看:37
本文介绍了向 django-registration 表单添加额外的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为组织"的模型,我已将其设置为用户个人资料,我希望组织"模型中的字段显示在注册页面上.我如何使用 django-registration 来执行此操作.

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-registration 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/'] 添加:

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}),
)

显然,您也可以创建自定义后端,但恕我直言,这更容易.

Obviously, you can also create a custom backend, but IMHO this is way easier.

这篇关于向 django-registration 表单添加额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆