【发布时间】: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