設定
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'),