gpt4 book ai didi

python - 添加last_name,first_name到默认的django_registration(不是redux)

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:19 26 4
gpt4 key购买 nike

我使用 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:

抱歉我的英语很糟糕。

最佳答案

如果您使用简单后端,则应该从该后端对 View 进行子类化。您当前使用的是基本 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 可能会取得更大成功。

关于python - 添加last_name,first_name到默认的django_registration(不是redux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com