我正在使用django-registration和django-profile来处理注册和个人资料。我希望在注册时为用户创建个人资料。我已经创建了自定义的注册表单,并按照教程将其添加到urls.py中:
在这个教程中的基本想法是覆盖默认的注册表单,同时创建个人资料。forms.py - 在我的个人资料应用程序中。
from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from profiles.models import UserProfile
from registration.models import RegistrationProfile
attrs_dict = { 'class': 'required' }
class UserRegistrationForm(RegistrationForm):
city = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
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 = UserProfile(user=new_user, city=self.cleaned_data['city'])
new_profile.save()
return new_user
在 urls.py 文件中。
from profiles.forms import UserRegistrationForm
并且
url(r'^register/$',
register,
{'backend': 'registration.backends.default.DefaultBackend', 'form_class' : UserRegistrationForm},
name='registration_register'),
表单已显示,我可以输入城市,但它不会保存或在数据库中创建条目。