我使用 django-registration 1.0(我不确定,但我知道这是最新版本)所以,我想让用户在注册期间输入他自己的名字和姓氏,并将它们与其他(用户名、电子邮件、密码)发送给用户模型。所以我在 forms.py 中创建了自己的表单,它继承了默认的 django RegistrationForm,并添加了一些额外的字段
from registration.forms import RegistrationForm
from django.forms.fields import CharField
from django.contrib.auth.models import User
class UpdatedRegistrationForm(RegistrationForm):
first_name = CharField(max_length=20)
last_name = CharField(max_length=30)
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
def save(self, commit = True):
user = super(UpdatedRegistrationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
return user
然后我将新表单放入urls.py
from django.conf.urls import include, url
from django.contrib import admin
from accounts.views import home_page, admin_panel
from accounts.forms import UpdatedRegistrationForm
from registration.views import RegistrationView
class RegistrationViewUniqueEmail(RegistrationView):
form_class = UpdatedRegistrationForm
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^home/', home_page),
url(r'^admin_pannel/',admin_panel),
url(r'^register/$', RegistrationViewUniqueEmail.as_view(), name='registration_register'),
url(r'', include('registration.backends.simple.urls')),
url(r'', include('social_auth.urls')),
]
当我运行我的服务器并访问 127.0.0.1:8000/register/ 时,我看到,额外的字段已添加到我的表单中 register form
然后我按注册,然后我看到一个错误 error 1但我也看到数据已发送。所以,我认为原因可能是没有重写 init() 方法。但我不知道该怎么做,所以可能还有另一个原因。请在这件事上给予我帮助。错误:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/register/
Django Version: 1.8.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'social_auth',
'accounts',
'rest_framework')
Installed Middleware:
('accounts.middleware_log.MiddleWareLog',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist- packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist- packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/registration/views.py" in dispatch
79. return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist- packages/django/views/generic/base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/registration/views.py" in post
35. return self.form_valid(request, form)
File "/usr/local/lib/python2.7/dist-packages/registration/views.py" in form_valid
82. new_user = self.register(request, **form.cleaned_data)
File "/usr/local/lib/python2.7/dist-packages/registration/views.py" in register
109. raise NotImplementedError
Exception Type: NotImplementedError at /register/
Exception Value:
抱歉我的英语很糟糕。
我是一名优秀的程序员,十分优秀!