且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向 django-registration 表单添加额外的字段

更新时间:2023-12-02 08:37:04

最简单的方法是[在 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.