【发布时间】:2011-02-05 18:53:12
【问题描述】:
我正在使用 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'),
显示表格,我可以在城市中输入,但它不会在数据库中保存或创建条目。
【问题讨论】:
-
Solution with signals - 这里我写了如何使用信号来保存额外的数据
标签: python django django-forms registration profile