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

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

问题描述

我有一个名为组织的模型,我已经设置为用户配置文件,我想让组织模型中的字段显示在注册页面上。如何使用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屋!

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