我已经查看了所有django注册示例,并且我已经成功创建了一个包含扩展字段的表单,但我遇到了下面列出的一些问题,主要是数据没有保存在DB表UserProfile中。
我创建了一个forms.py并存储在我的proj目录中。
如下:
from django import forms
from r2.models import Keyword
from r2.models import UserProfile
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from django.utils.translation import ugettext_lazy as _
from registration.forms import RegistrationForm, attrs_dict
class ProjectSpecificRegistrationForm(RegistrationForm):
keywords = forms.ModelChoiceField(queryset=Keyword.objects.all())
first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name'))
last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name'))
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,username=self.cleaned_data['username'], keywords_subscribed=self.cleaned_data['keywords'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name'])
new_profile.save()
和 URLS.py:
#registrations URLS
url(r'^activate/(?P<activation_key>\w+)/$',activate,name='registration_activate'),
url(r'^login/$',auth_views.login,{'template_name': 'registration/login.html'}, name='auth_login'),
url(r'^logout/$',auth_views.logout,{'template_name': 'registration/logout.html'},name='auth_logout'),
url(r'^password/change/$',auth_views.password_change,name='auth_password_change'),
url(r'^password/change/done/$',auth_views.password_change_done,name='auth_password_change_done'),
url(r'^password/reset/$',auth_views.password_reset,name='auth_password_reset'),
url(r'^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',auth_views.password_reset_confirm,name='auth_password_reset_confirm'),
url(r'^password/reset/complete/$',auth_views.password_reset_complete,name='auth_password_reset_complete'),
url(r'^password/reset/done/$',auth_views.password_reset_done,name='auth_password_reset_done'),
url(r'^register/$',register, {'backend': 'accounts.regbackend.RegBackend','form_class' : ProjectSpecificRegistrationForm}, name='registration_register'),
url(r'^register/complete/$',direct_to_template,{'template': 'registration/registration_complete.html'},name='registration_complete'),
我已经将所有这些添加到proj urls.py中,因为我已经从某个地方学习过教程。
问题1:即使我使用django注册0.6,用户个人资料也没有保存到数据库中。
问题2:这是设置urls.py的正确方法吗?因为我知道我不应该覆盖注册urls.py。
问题3:我必须创建regbackend.py吗?如果是这样我该怎么做呢?