添加 last_name , first_name 到默认 django_registration(不是 redux)
Adding last_name , first_name to default django_registration(not redux)
我使用 django-registration 1.0(我不确定,但我知道这是最新版本)所以,我想让用户输入他自己的 first_name 和 last_name,在他注册期间,将它们与其他(用户名、电子邮件、密码)一起存储到 User
模型。所以我创建了自己的表单,在 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:
对不起,我的英语很糟糕。
如果您使用的是简单后端,则应从该后端继承视图。您目前正在使用基础 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 可能会更成功。
我使用 django-registration 1.0(我不确定,但我知道这是最新版本)所以,我想让用户输入他自己的 first_name 和 last_name,在他注册期间,将它们与其他(用户名、电子邮件、密码)一起存储到 User 模型。所以我创建了自己的表单,在 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:
对不起,我的英语很糟糕。
如果您使用的是简单后端,则应从该后端继承视图。您目前正在使用基础 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 可能会更成功。