NameError: name 'LoginView' is not defined

NameError: name 'LoginView' is not defined

我正在按照网站上的教程安装 Openstack Stein Horizo​​n 服务(仪表板)。 由于 Django 版本,我遇到了一个错误,并使用此解释 Switch from django string_concat to format_lazy 修复了它。

下一个出现的问题是 AttributeError: module 'django.contrib.auth.views' has no attribute 'login',我用 google 搜索了一下,结果又是 Django 版本不兼容。 我在这里遵循了这个解释:AttributeError: module Django.contrib.auth.views has no attribute

openstack_auth/urls.py

from django.conf.urls import url
from django.views import generic

from openstack_auth import utils
from openstack_auth import views
from . import views
from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r"^login/$", LoginView.as_view(), name='login'),
    url(r"^logout/$", LogoutView.as_view(), name='logout'),
    url(r'^switch/(?P<tenant_id>[^/]+)/$', views.switch,
        name='switch_tenants'),
    url(r'^switch_services_region/(?P<region_name>[^/]+)/$',
        views.switch_region,
        name='switch_services_region'),
    url(r'^switch_keystone_provider/(?P<keystone_provider>[^/]+)/$',
        views.switch_keystone_provider,
        name='switch_keystone_provider')
]

if utils.is_websso_enabled():
    urlpatterns += [
        url(r"^websso/$", views.websso, name='websso'),
        url(r"^error/$",
            generic.TemplateView.as_view(template_name="403.html"))
    ]

apache2/error.log

   Traceback (most recent call last):
   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/wsgi.py", line 141, in __call__
     response = self.get_response(request)
   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 75, in get_response
     response = self._middleware_chain(request)
   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 36, in inner
     response = response_for_exception(request, exc)
   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 90, in response_for_exception
     response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 128, in handle_uncaught_exception
     callback, param_dict = resolver.resolve_error_handler(500)
   File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 584, in resolve_error_handler
     callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
   File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 80, in __get__
     res = instance.__dict__[self.name] = self.func(instance)
   File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 564, in urlconf_module
     return import_module(self.urlconf_name)
   File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
   File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
   File "/usr/lib/python3/dist-packages/openstack_dashboard/urls.py", line 53, in <module>
     urlpatterns.append(url(r'^auth/', include(u)))
   File "/usr/local/lib/python3.6/dist-packages/django/urls/conf.py", line 34, in include
     urlconf_module = import_module(urlconf_module)
   File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
   File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
   File "/usr/lib/python3/dist-packages/openstack_auth/urls.py", line 23, in <module>
     url(r"^login/$", LoginView.as_view(), name='login'),
   NameError: name 'LoginView' is not defined

Django 版本:2.2

谢谢。

您确实将 django.contrib.auth.views 模块导入为 auth_views,而不是该模块中的 LoginViewLogoutView 等。因此,您可以使用 auth_views.LoginView:

from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r"^login/$", <b>auth_views.</b>LoginView.as_view(), name='login'),
    url(r"^logout/$", <b>auth_views.</b>LogoutView.as_view(), name='logout'),
    url(r'^switch/(?P<tenant_id>[^/]+)/$', views.switch,
        name='switch_tenants'),
    url(r'^switch_services_region/(?P<region_name>[^/]+)/$',
        views.switch_region,
        name='switch_services_region'),
    url(r'^switch_keystone_provider/(?P<keystone_provider>[^/]+)/$',
        views.switch_keystone_provider,
        name='switch_keystone_provider')
]

或者您可以从该模块导入 LoginViewLogoutView

from django.contrib.auth<b>.views import LoginView, LogoutView</b>

urlpatterns = [
    url(r"^login/$", LoginView.as_view(), name='login'),
    url(r"^logout/$", LogoutView.as_view(), name='logout'),
    url(r'^switch/(?P<tenant_id>[^/]+)/$', views.switch,
        name='switch_tenants'),
    url(r'^switch_services_region/(?P<region_name>[^/]+)/$',
        views.switch_region,
        name='switch_services_region'),
    url(r'^switch_keystone_provider/(?P<keystone_provider>[^/]+)/$',
        views.switch_keystone_provider,
        name='switch_keystone_provider')
]