将 last_name , first_name 添加到默认 django_registration(不是 redux)
Posted
技术标签:
【中文标题】将 last_name , first_name 添加到默认 django_registration(不是 redux)【英文标题】:Adding last_name , first_name to default django_registration(not redux) 【发布时间】:2016-02-20 15:07:37 【问题描述】:我使用 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:
对不起,我的英语很糟糕。
【问题讨论】:
所以,我编辑了我的帖子,我认为我使用的是 django-registration 1.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 可能会更成功。
【讨论】:
以上是关于将 last_name , first_name 添加到默认 django_registration(不是 redux)的主要内容,如果未能解决你的问题,请参考以下文章
在 Django 用户模型中需要 first_name 和 last_name
试图在 Oracle SQL 中将 FULL_NAME 分成 FIRST_NAME 和 LAST_NAME,但它没有显示表本身的任何更改
19查找所有员工的last_name和first_name以及对应的dept_name
将变量值传递给 paypal API - first_name 和 last_name