0

私は Django 登録 (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」パラメーターを「register」ビューに追加して、「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」パラメーターに渡す方法です。変数「性別」、「国」、「都市」を参照するにはどうすればよいですか?

前もって感謝します。

4

1 に答える 1

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
于 2012-11-17T19:00:44.783 に答える