我使用的是django的旧版本。运行代码后,出现此错误-“ AttributeError:模块'django.contrib.auth.views'没有属性'LoginView'”。如果使用登录而不是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"
我在进行迁移后得到了这个结果。我的应用程序的名称是account:-
(myDjangoEnv) C:\Users\saini computers\Desktop\simple_social_clone\simplesocial>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:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 342, in execute
self.check()
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\importlib\__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
simplesocial\accounts\views.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:\Users\saini computers\Desktop\simple_social_clone\simplesocial\simplesocial\urls.py", line 23, in <module>
url(r'^accounts/',include('accounts.urls',namespace='accounts')),
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\importlib\__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:\Users\saini computers\Desktop\simple_social_clone\simplesocial\accounts\urls.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'
答案 0 :(得分:1)
LoginView
已在Django 1.11中添加。如果导入失败,则必须使用旧版本的Django。
Django 1.11 LTS是受支持的最旧版本的Django,因此您确实应该升级,最好是升级到最新版本2.2。
答案 1 :(得分:1)
最好说出您使用的是哪个版本。无论如何,类“ LoginView”是在Django 1.11(https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.views.LoginView)中实现的
如果您的版本与此版本兼容,请尝试将url部分更改为:
path('accounts/login/',auth_views.LoginView.as_view(template_name='myapp/login.html'))
希望有帮助