我正在按照网站上的教程安装Openstack Stein Horizon服务(仪表板(。我遇到了一个由Django版本引起的错误,并使用以下解释进行了修复:从Django-string_conat切换到format_lazy。
出现的下一个问题是AttributeError:模块"django.contrib.auth.views"没有属性"login">,我在谷歌上搜索了一下,结果再次发现django版本不兼容。我在这里遵循了这样的解释:AttributeError:模块Djang.contrib.auth.views没有属性
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
Thks。
您确实将django.contrib.auth.views
模块导入为auth_views
,而不是从该模块导入LoginView
、LogoutView
等。例如,您可以使用auth_views.LoginView
:
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r"^login/$",auth_views.LoginView.as_view(), name='login'),
url(r"^logout/$",auth_views.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')
]
或者您可以从该模块导入LoginView
和LogoutView
:
from django.contrib.auth.views import LoginView, LogoutView
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')
]