我正在使用Django註冊(https://bitbucket.org/ubernostrum/django-registration/),我需要添加一些字段用戶註冊。Django註冊 - 如何從子類傳遞「extra_context」參數表格
我已經做了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 =
爲了使它工作,我已經改變了「urls.py」,顯示通過添加「form_class」參數設置爲「註冊」「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」中的所有額外字段(性別,國家,城市)都是NOT存儲在數據庫中。
閱讀文檔http://docs.b-list.org/django-registration/0.8/views.html#registration.views.register似乎我必須將參數「extra_context」傳遞給視圖。
我的問題是如何將字典傳遞給「extra_context」參數。如何引用變量「性別」,「國家」和「城市」?
在此先感謝。
你是否已在擴展註冊表單中定義了'save'方法? –