Python AttributeError:模块Django.contrib.auth.views没有属性
在我的Django应用程序useraccounts中,我为我的注册创建了一个注册表单和一个模型。但是,当我运行python manage.py makemigrations时,我遇到了错误:AttributeError:module Django.contrib.auth.views没有属性“registration”。其次,我是否在forms.py中正确编码了注册表单?我不想在模型中使用用户模型,因为它会要求用户名,我不想我的网站要求用户名 这是我的密码: models.pyPython AttributeError:模块Django.contrib.auth.views没有属性,python,django,django-models,django-forms,attributeerror,Python,Django,Django Models,Django Forms,Attributeerror,在我的Django应用程序useraccounts中,我为我的注册创建了一个注册表单和一个模型。但是,当我运行python manage.py makemigrations时,我遇到了错误:AttributeError:module Django.contrib.auth.views没有属性“registration”。其次,我是否在forms.py中正确编码了注册表单?我不想在模型中使用用户模型,因为它会要求用户名,我不想我的网站要求用户名 这是我的密码: models.py from dja
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
class UserProfile(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.EmailField(max_length=150)
birth_date = models.DateField()
password = models.CharField(max_length=150)
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
instance.profile.save()
forms.py
from django.forms import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from useraccounts.models import UserProfile
class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ('first_name',
'last_name',
'email',
'password1',
'password2', )
views.py
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from useraccounts.forms import SignUpForm
# Create your views here.
def home(request):
return render(request, 'useraccounts/home.html')
def login(request):
return render(request, 'useraccounts/login.html')
def registration(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save()
user.refresh_from_db()
user.profile.birth_date = form.cleaned_data.get('birth_date')
user.save()
raw_password = form.cleaned_data.get('password1')
user = authenticate(password=raw_password)
login(request, user)
return redirect('home')
else:
form = SignUpForm()
return render(request, 'registration.html', {'form': form})
url.py
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', views.home),
url(r'^login/$', auth_views.login, {'template_name': 'useraccounts/login.html'}, name='login'),
url(r'^logout/$', auth_views.logout, {'template_name': 'useraccounts/logout.html'}, name='logout'),
url(r'^registration/$', auth_views.registration, {'template_name': 'useraccounts/registration.html'}, name='registration'),
]
应该是:
url(r'^registration/$', views.registration, {'template_name': 'useraccounts/registration.html'}, name='registration'),
from django.contrib.auth import views as auth_views
urlpatterns = [
url( r'^login/$',auth_views.LoginView.as_view(template_name="useraccounts/login.html"), name="login"),
]
auth_视图没有注册,您的视图有注册。我不能留下评论,所以我决定留下答案。 在else块附近有额外的缩进。您的注册函数应如下所示:
def registration(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save()
user.refresh_from_db()
user.profile.birth_date = form.cleaned_data.get('birth_date')
user.save()
raw_password = form.cleaned_data.get('password1')
user = authenticate(password=raw_password)
login(request, user)
return redirect('home')
else:
form = SignUpForm()
return render(request, 'registration.html', {'form': form})
这就是为什么会出现这个错误
view useraccounts.views.registration未返回HttpResponse对象。它没有返回任何结果
您的urlpatterns应该是:
url(r'^registration/$', views.registration, {'template_name': 'useraccounts/registration.html'}, name='registration'),
from django.contrib.auth import views as auth_views
urlpatterns = [
url( r'^login/$',auth_views.LoginView.as_view(template_name="useraccounts/login.html"), name="login"),
]
打开
url.py
并替换:
django.contrib.auth.views.logindjango.contrib.auth.views.LoginView
django.contrib.auth.views.logout
django.contrib.auth.views.LogoutView
在django版本2.1中,使用auth应用程序使用的自定义URL模式
from django.urls import path, re_path
from django.contrib.auth import views as auth_views
from django.conf import settings
from .views import register_view, activate
urlpatterns = [
# url(r'^$', HomeView.as_view(), name='home'),
re_path(r'^register/$', register_view, name='signup'),
re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
activate, name='users_activate'),
re_path('login/', auth_views.LoginView, {
'template_name': "users/registration/login.html"},
name='login'),
re_path('logout/', auth_views.LogoutView,
{'next_page': settings.LOGIN_REDIRECT_URL}, name='logout'),
re_path(r'^password_reset/$', auth_views.PasswordResetView,
{'template_name': "users/registration/password_reset_form.html"},
name='password_reset'),
re_path(r'^password_reset/done/$', auth_views.PasswordResetDoneView,
{'template_name': "users/registration/password_reset_done.html"},
name='password_reset_done'),
re_path(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
auth_views.PasswordResetConfirmView,
{'template_name': "users/registration/password_reset_confirm.html"},
name='password_reset_confirm'),
re_path(r'^reset/done/$', auth_views.PasswordResetCompleteView,
{'template_name': "users/registration/password_reset_complete.html"},
name='password_reset_complete'),
]
从django.url导入路径,重新设置路径
从django.contrib.auth导入视图作为auth_视图
从django.conf导入设置
从.views导入寄存器\u视图,激活
URL模式=[
#url(r'^$',HomeView.as_view(),name='home'),
重新注册路径(r'^register/$',register_view,name='signup'),
重路径(r'^activate/(?P[0-9A-Za-z_ \-])/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$,
激活,name='users\u activate'),
重新设置路径('login/',auth_views.LoginView{
“模板名称”:“users/registration/login.html”},
name='login'),
重新设置路径('logout/',auth_views.LogoutView,
{'next\u page':settings.LOGIN\u REDIRECT\u URL},name='logout'),
重新设置路径(r“^password\u reset/$”,auth\u views.PasswordResetView,
{'template_name':“users/registration/password_reset_form.html”},
name='password_reset'),
重新设置路径(r'^password'\u reset/done/$,auth_views.PasswordResetDoneView,
{'template_name':“users/registration/password_reset_done.html”},
name='password\u reset\u done'),
重路径(r'^reset/(?P[0-9A-Za-z_ \-])/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$,
auth_views.PasswordResetConfirmView,
{'template_name':“users/registration/password_reset_confirm.html”},
name='password\u reset\u confirm'),
重新设置路径(r“^reset/done/$”,验证视图。密码resetcompleteView,
{'template_name':“users/registration/password_reset_complete.html”},
name='password\u reset\u complete',
]
打开url.py并替换:
更改视图。login
=>视图。LoginView.as_view()
您需要将LoginView
等作为一个类,而不是一个函数(Django 1.11中新增的应为且必须从2.1版开始)Django 2.1 contrib视图从函数视图更改为类视图,并且名称也会更改,因此在此过程中,您需要指定其他视图名称
url.py
设置.py
为自定义用户模型指定所需的设置
# AUTH USER MODEL
AUTH_USER_MODEL = "Accounts.User"
LOGIN_URL = '/login/'
#LOGIN_REDIRECT_URL = 'login_success'
LOGOUT_REDIRECT_URL = '/login/'
管理员
管理员您需要注册用户模型
## user model view
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _
@admin.register(User)
class UserAdmin(DjangoUserAdmin):
"""Define admin model for custom User model with no email field."""
fieldsets = (
(None, {'fields': ('email', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email_confirmed')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'first_name', 'last_name'),
}),
)
list_display = ('email', 'first_name', 'last_name',)
search_fields = ('email', 'first_name', 'last_name')
ordering = ('-id',)
非常简单的步骤:
转到项目url.py
将视图。登录更改为视图。LoginView.as_view()
如果您使用的是注销属性,则对该文件执行相同的操作,然后打开url.py
文件并替换
视图。使用视图登录
。登录视图
您必须在URL中导入登录视图。py:
from django.contrib.auth.views import LoginView
改变
auth_view.login
到
请发布您的完整错误回溯结果表明我在注册时弄乱了url.py部分。谢谢你的回答!始终编写Django版本,因为版本中正在进行大量开发。谢谢。但是现在当我打开useraccounts/registration.html时,我得到了一个错误:“视图useraccounts.views.registration没有返回一个HttpResponse对象。它反而没有返回任何对象。”另外,我是否正确地执行了表单和视图?您是否可以添加一些上下文以使您的答案更合适?e、 代码是什么?您正在用类替换函数,因此语法可能不同。您可能还想提到,这只适用于Django>=1.11I从2升级到2.2.3时,Django 2中出现了此错误。我修复了我的网址,就像。。。url(r'^accounts/login/$,django.contrib.auth.views.LoginView,name=“login”),url(r'^accounts/logout/$,django.contrib.auth.views.LogoutView,{'next_page':'/'),name=“logout”),
LoginView.as_view()