我正在使用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
Run Code Online (Sandbox Code Playgroud)
在urls.py中
from profiles.forms import UserRegistrationForm
Run Code Online (Sandbox Code Playgroud)
和
url(r'^register/$',
register,
{'backend': 'registration.backends.default.DefaultBackend', 'form_class' : UserRegistrationForm},
name='registration_register'),
Run Code Online (Sandbox Code Playgroud)
显示表单,我可以在City中输入,但不会在DB中保存或创建条目.