我使用django-registration 1.0(我不确定它,但我知道它是lates版本)所以,我想让用户在注册期间输入他自己的first_name和last_name,并将它们存储在其他地方(用户名,电子邮件,传递给用户 模型。所以我创建了自己的表单,在 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:
对不起我非常糟糕的英语。
答案 0 :(得分:1)
如果您使用简单的后端,则应该从该后端继承视图。您目前使用的基础RegistrationView
未实现register
方法。
而不是导入
from registration.views import RegistrationView
你应该试试
from registration.backends.simple.views import RegistrationView
django-registration将有希望have a new release soon,但目前它还没有新版本发布一段时间。最新版本可能不适用于最新版本的Django,在这种情况下,您可能会在django-registration-redux上取得更大成功。