3

簡単な質問があります。これは私のプロフィールです:

class Profile(models.Model):

    user = models.ForeignKey(User, unique=True)
    born = models.DateTimeField('born to')    
    photo = models.ImageField(upload_to='profile_photo')

これらのフィールド (fromおよびmodels)を含む登録フォームを作成したい:UserProfile

  • ユーザー名
  • ファーストネーム
  • 苗字
  • 生まれ
  • 写真

これらのフィールドは必須です。

それ、どうやったら出来るの?

get_profile()この課題のテンプレートではどのように機能しますか?

ありがとうございました :)

4

1 に答える 1

9

設定

django-profilesおよびdjango-registrationプロジェクトを使用していますか? そうでない場合は、このコードの多くが既に作成されているはずです。

プロフィール

あなたのユーザー プロファイル コードは次のとおりです。

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    born = models.DateTimeField('born to')    
    photo = models.ImageField(upload_to='profile_photo')

Django 設定でこのプロファイルを正しく設定しましたか? yourappそうでない場合は、アプリの名前に置き換えて、これを追加する必要があります。

AUTH_PROFILE_MODULE = "yourapp.Profile"

登録用紙

django-registrationにはいくつかのデフォルトの登録フォームが付属していますが、独自の登録フォームを作成することを指定しました。各Django フォーム フィールドはデフォルトで必須になっているため、変更する必要はありません。重要な部分は、既存の登録フォーム フィールドを確実に処理し、プロファイルの作成に追加することです。このようなものが動作するはずです:

from django import forms
from registration.forms import RegistrationForm
from yourapp.models import Profile
from registration.models import RegistrationProfile

class YourRegistrationForm(RegistrationForm):
    born = forms.DateTimeField()
    photo = forms.ImageField()

    def save(self, profile_callback=None):
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
        password=self.cleaned_data['password1'],
        email = self.cleaned_data['email'])
        new_profile = Profile(user=new_user, born=self.cleaned_data['born'], photo=self.cleaned_data['photo'])
        new_profile.save()
        return new_user

まとめる

デフォルトのdjango-registrationテンプレートとビューを使用できますが、フォームに渡す必要がありますurls.py

from registration.backends.default import DefaultBackend
from registration.views import activate
from registration.views import register

# ... the rest of your urls until you find somewhere you want to add ...

url(r'^register/$', register,
    {'form_class' : YourRegistrationForm, 'backend': DefaultBackend},
    name='registration_register'),
于 2010-01-06T04:07:52.970 に答える