我的代码@Python django博客有问题获取此错误:AttributeError:模块"django.contrib.auth.admin"没有属性"site"错误如下
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/martin/My-blog/mysite/urls.py", line 6, in <module>
path(r'^admin/', admin.site.urls),
AttributeError: module 'django.contrib.auth.admin' has no attribute 'site'
我的urls.py如下
from django.urls import include, path
from django.contrib.auth import login
from django.contrib.auth import admin
urlpatterns =[
path(r'^admin/', admin.site.urls),
path(r'^accounts/login/$', 'django.contrib.auth.views.login'),
path(r'^accounts/logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
path(r'', 'blog.urls'),
]
admin
不是auth
模块的子模块,它是django.contrib
的子模块。因此,您导入这个:
from django.urls import include, path
from django.contrib.auth.views import LoginView, LogoutView
from django.contrib.admin import site
urlpatterns = [
path('admin/',site.urls),
path('accounts/login/', LoginView.as_view()),
path('accounts/logout/', LogoutView.as_view(next_page='/')),
path('', include('blog.urls')),
]