2012-11-17 35 views
0

我正在使用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」參數。如何引用變量「性別」,「國家」和「城市」?

在此先感謝。

+0

你是否已在擴展註冊表單中定義了'save'方法? –

回答

0

您忘記在您的擴展註冊表中有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 
+0

嗨,阿米爾阿德南!感謝您的回覆。我用裏面的「pdb.set_trace()」測試了保存方法。代碼不會在保存方法內輸入。什麼叫這個保存方法?我怎樣才能讓代碼進入保存方法?再次感謝。 –

+1

性別,國家和城市字段甚至呈現在模板中嗎? –

+0

是的,呈現。我看到價值傳遞。問題不在於形式。我正在閱讀Django文檔。我應該如下所述創建一個「def create_user_profile」嗎? https://docs.djangoproject.com/en/1.4/topics/auth/#storing-additional-information-about-users –