我的代码遇到问题@Python django博客收到此错误:属性错误:模块"django.contrib.auth.admin"没有属性"site"



我的代码@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')),
]

相关内容

  • 没有找到相关文章

最新更新