AttributeError:模块'django.contrib.auth.views'没有属性'LoginView'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AttributeError:模块'django.contrib.auth.views'没有属性'LoginView'相关的知识,希望对你有一定的参考价值。

我正在使用旧版本的django.A运行我的代码后我得到了这个错误 - “AttributeError:module'django.contrib.auth.views'没有属性'LoginView'”。如果我使用login而不是LoginView。即使在使用登录后,我也会遇到相同的属性错误。

这是我的urls.py-

    from django.conf.urls import url
    from django.contrib.auth import views as auth_views
    from . import views

    app_name = 'accounts'

        urlpatterns = [

url(r"login/$",auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
        url(r"logout/$", auth_views.LogoutView.as_view(), name="logout"),
        url(r"signup/$", views.SignUp.as_view(), name="signup"),
    ]

这是我的应用程序的urls.py-

from django.conf.urls import url,include
from django.contrib import admin
from .import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',views.HomePage.as_view(),name='home'),
    url(r'^accounts/',include('accounts.urls',namespace='accounts')),
    url(r'^accounts',include('django.contrib.auth.urls')),
    url(r'^test/$',views.TestPage.as_view(),name='test'),
    url(r'^thanks/$',views.ThanksPage.as_view(),name='thanks')
]

和views.py-

from django.shortcuts import render

from django.contrib.auth import login, logout
from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView

from . import forms

class SignUp(CreateView):
    form_class = forms.UserCreateForm
    success_url = reverse_lazy("login")
    template_name = "accounts/signup.html"

在进行迁移后我得到了这个结果。我的应用程序名称是帐户: -

(myDjangoEnv) C:Userssaini computersDesktopsimple_social_clonesimplesocial>python manage.py makemigrations accounts
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocoremanagement\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocoremanagement\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocoremanagementase.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocoremanagementase.py", line 342, in execute
    self.check()
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocoremanagementase.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocoremanagementase.py", line 361, in _run_checks

    return checks.run_checks(**kwargs)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocorechecks
egistry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocorechecksurls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangocorechecksurls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangoutilsfunctional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangourls
esolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangoutilsfunctional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangourls
esolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibimportlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
Git
GitHub
Initialize a new project directory with a Git repository
Create repository
simplesocialaccountsviews.py14:1(13, 372)
LFUTF-8PythonGitHubGit (0)
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:Userssaini computersDesktopsimple_social_clonesimplesocialsimplesocialurls.py", line 23, in <module>
    url(r'^accounts/',include('accounts.urls',namespace='accounts')),
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibsite-packagesdjangoconfurls\__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "C:Userssaini computersAnaconda3envsmyDjangoEnvlibimportlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:Userssaini computersDesktopsimple_social_clonesimplesocialaccountsurls.py", line 8, in <module>
    url(r"login/$", auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
AttributeError: module 'django.contrib.auth.views' has no attribute 'LoginView'
答案

LoginView被添加到Django 1.11中。如果导入失败,则必须使用旧版本的Django。

Django 1.11 LTS是最早支持的Django版本,所以你真的应该升级,理想情况下升级到2.2版。

另一答案

说出您正在使用的版本始终是一个好主意。无论如何,类“LoginView”在Django 1.11(https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.views.LoginView)中实现

如果您的版本与之兼容,请尝试将网址部分更改为:

path('accounts/login/',auth_views.LoginView.as_view(template_name='myapp/login.html'))

希望能帮助到你

以上是关于AttributeError:模块'django.contrib.auth.views'没有属性'LoginView'的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:模块'django.contrib.auth.views'没有属性'LoginView'

AttributeError:模块'theano'没有属性'ifelse'

AttributeError:模块'win32ctypes.pywin32.win32api'没有属性'错误'

AttributeError:模块'dis'没有属性'_unpack_opargs'

AttributeError:模块'tensorflow'没有带Ke ras的属性'name_scope'

解决AttributeError:模块'pandas'没有使用command-prompt或pycharm属性'core'