我正在尝试向用户添加一些扩展字段。所以我像你一样添加了一个UserProfile类。
我还创建了一个UserProfile表单:
class UserProfileForm(RegistrationFormUniqueEmail):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
class Meta:
model = UserProfile
fields = ['gender', 'phone']
然后设置自定义RegistrationView:
from registration.backends.default.views import RegistrationView
from forms import UserProfileForm
class UserRegistrationView(RegistrationView):
form_class = UserProfileForm
def register(self, request, form_class):
new_user = super(UserRegistrationView, self).register(request, form_class)
new_user.first_name = form_class.cleaned_data['first_name']
new_user.last_name = form_class.cleaned_data['last_name']
new_user.save()
user_profile = new_user.get_profile()
user_profile.gender = form_class.cleaned_data['gender']
user_profile.phone = form_class.cleaned_data['phone']
user_profile.save()
return user_profile
我将自定义视图添加到我应用的路线中:
from django.conf.urls import include, url
from django.contrib import admin
from userprofile import regbackend
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/register/$', regbackend.UserRegistrationView.as_view(), name='registration_register'),
url(r'^accounts/', include('registration.backends.default.urls')),
]
但是,当我尝试注册新用户时,我收到以下错误: / accounts / register /' UserProfile'对象没有属性' set_password'
知道问题是什么吗?