【问题标题】:Django-registration - How to pass the "extra_context" parameter from a subclass FormDjango-registration - 如何从子类表单传递“extra_context”参数
【发布时间】:2026-02-03 17:30:01
【问题描述】:

我使用的是Django Registration(https://bitbucket.org/ubernostrum/django-registration/),需要在用户的注册中添加一些字段。

我已经创建了 RegistrationForm 的子类。我的“forms.py”如下:

from django.contrib.auth.models import User
from myproject.apps.userprofile.models import Gender, UserProfile
from myproject.apps.location.models import Country, City
from django import forms
from registration.forms import RegistrationForm
from registration.models import RegistrationManager


class RegistrationFormExtend(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which adds the fiedls in the userprofile app
    """   
    gender              = forms.ModelChoiceField(queryset=Gender.objects.all(), empty_label="(Nothing)")
    country             = forms.ModelChoiceField(queryset=Country.objects.all(), empty_label="(Nothing)")
    city                = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="(Nothing)")
    #profile_picture     = 

为了使其正常工作,我通过将“form_class”参数添加到“注册”视图来更改“urls.py”以显示“RegistrationFormExtend”表单:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

from registration.views import activate
from registration.views import register

from myproject.apps.registrationextend.forms import RegistrationFormExtend


urlpatterns = patterns('',
               ...
                   url(r'^registar/$',
                       register,
                       {'backend': 'registration.backends.default.DefaultBackend', 'form_class': RegistrationFormExtend,},
                       name='registration_register'),
                   ...
                   )

在那之后,我进行了测试并且表单正在工作。用户注册成功,但“RegistrationFormExtend”中的所有额外字段(性别、国家、城市)都存储在数据库中。

阅读文档,http://docs.b-list.org/django-registration/0.8/views.html#registration.views.register 看来我必须将参数“extra_context”传递给视图。

我的问题是如何将字典传递给“extra_context”参数。如何引用变量“gender”、“country”和“city”?

提前致谢。

【问题讨论】:

  • 您是否在扩展注册表单中定义了save 方法?

标签: django django-registration


【解决方案1】:

您忘记在扩展注册表中使用save 方法。你必须这样做:

class RegistrationFormExtend(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which adds the fiedls in the userprofile app
    """   
    gender = forms.ModelChoiceField(queryset=Gender.objects.all(), empty_label="(Nothing)")
    country = forms.ModelChoiceField(queryset=Country.objects.all(), empty_label="(Nothing)")
    city = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="(Nothing)")
    #profile_picture

    def __init__(self, *args, **kw):
       super(RegistrationFormExtend, self).__init__(*args, **kw)

    def save(self):
        #first save the parent form
        profile = super(RegistrationFormExtend, self).save()
        #then save the custom fields
        #i am assuming that the parent form return a profile
        profile.gender = self.cleaned_data['gender']
        profile.country = self.cleaned_data['country']
        profile.city = self.cleaned_data['city']
        profile.save()
        return profile

【讨论】:

  • 嗨阿米尔阿德南!感谢您的回复。我已经用“pdb.set_trace()”测试了保存方法。代码不会进入 save 方法中。什么叫这个保存方法?如何使代码输入到保存方法中?再次感谢。
  • 性别、国家和城市字段是否在模板中呈现?
  • 是的,被渲染了。我看到价值观在传递。问题不在形式上。我正在阅读 Django 文档。我应该按照这里的描述创建一个“def create_user_profile”吗? docs.djangoproject.com/en/1.4/topics/auth/…
  • 可能这个*.com/questions/3114976/…会帮助更多,这个dewful.com/?p=70
  • 嗨阿米尔阿德南。我已经测试了您的新解决方案,但无法正常工作。我会阅读你给我的链接。非常感谢您的帮助。
最近更新 更多