Django 管理员/ return 404
Django admin/ return 404
启动开发服务器
未找到:/admin/
[30/May/2021 20:33:56] "GET /admin/ HTTP/1.1" 404 2097
project/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('marketability.mkbl_urls')),
path('admin/', admin.site.urls),
path(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
变体 path(r'admin/', admin.site.urls),
和 path(r'^admin/', admin.site.urls),
也不起作用。
project/marketability/mkbl_urls.py
from django.urls import path
from django.views.generic.base import RedirectView, TemplateView
from . import views
app_name = 'marketability'
handler404 = 'marketability.views.handler404'
urlpatterns = [
path('', views.home, name="home"),
path('<slug:cat_>/', views.page_Category_Main, name='cat'),
path('<slug:cat_>/<slug:product_>', views.page_Product, name='product'),
path('al_about.html', views.about, name="about"),
path('al_home.html', views.home, name="home"),
path('search_all.html', views.search_all, name="doorway"),
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain")),
path('sitemap.xml', TemplateView.as_view(template_name="sitemap.xml", content_type="text/xml")),
path('favicon.ico', RedirectView.as_view(url='/static/marketability/favicon.ico', permanent=True)),
]
project/marketability/admin.py
from django.contrib import admin
from .models import TxtHow, TxtRatings
# Register your models here.
admin.site.register(TxtHow)
admin.site.register(TxtRatings)
project/settings.py
....
NSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marketability',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/marketability/patterns'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
....
superuser
已创建
因此,我没有发现与 Django 文档有任何差异。
如何解决?
谢谢
你的路径:
path('<<b>slug:cat_</b>>/', views.page_Category_Main, name='cat'),
将与 admin/
匹配,因此将 admin
视为 cat_
slug 的值,从而触发该视图。该路径的视图可能会尝试获取带有 admin
作为 slug 的元素,但未能执行此操作,从而引发 HTTP 404 响应。
您可以将 admin/
和 ckeditor/
的 url 放在您的类别之前:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
<b>path('admin/', admin.site.urls)</b>,
<b>path('ckeditor/', include('ckeditor_uploader.urls'))</b>,
path('', include('marketability.mkbl_urls')),
]
Django 将自上而下地访问 url 模式,从而触发第一个匹配的路径模式,在这种情况下是管理员。
然而,最好也为您的 marketability
url 添加前缀,例如:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
path(<b>'market/'</b>, include('marketability.mkbl_urls')),
]
否则不可能有一个名为admin
的类别。
未找到:/admin/ [30/May/2021 20:33:56] "GET /admin/ HTTP/1.1" 404 2097
project/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('marketability.mkbl_urls')),
path('admin/', admin.site.urls),
path(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
变体 path(r'admin/', admin.site.urls),
和 path(r'^admin/', admin.site.urls),
也不起作用。
project/marketability/mkbl_urls.py
from django.urls import path
from django.views.generic.base import RedirectView, TemplateView
from . import views
app_name = 'marketability'
handler404 = 'marketability.views.handler404'
urlpatterns = [
path('', views.home, name="home"),
path('<slug:cat_>/', views.page_Category_Main, name='cat'),
path('<slug:cat_>/<slug:product_>', views.page_Product, name='product'),
path('al_about.html', views.about, name="about"),
path('al_home.html', views.home, name="home"),
path('search_all.html', views.search_all, name="doorway"),
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain")),
path('sitemap.xml', TemplateView.as_view(template_name="sitemap.xml", content_type="text/xml")),
path('favicon.ico', RedirectView.as_view(url='/static/marketability/favicon.ico', permanent=True)),
]
project/marketability/admin.py
from django.contrib import admin
from .models import TxtHow, TxtRatings
# Register your models here.
admin.site.register(TxtHow)
admin.site.register(TxtRatings)
project/settings.py
....
NSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marketability',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/marketability/patterns'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
....
superuser
已创建
因此,我没有发现与 Django 文档有任何差异。 如何解决? 谢谢
你的路径:
path('<<b>slug:cat_</b>>/', views.page_Category_Main, name='cat'),
将与 admin/
匹配,因此将 admin
视为 cat_
slug 的值,从而触发该视图。该路径的视图可能会尝试获取带有 admin
作为 slug 的元素,但未能执行此操作,从而引发 HTTP 404 响应。
您可以将 admin/
和 ckeditor/
的 url 放在您的类别之前:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
<b>path('admin/', admin.site.urls)</b>,
<b>path('ckeditor/', include('ckeditor_uploader.urls'))</b>,
path('', include('marketability.mkbl_urls')),
]
Django 将自上而下地访问 url 模式,从而触发第一个匹配的路径模式,在这种情况下是管理员。
然而,最好也为您的 marketability
url 添加前缀,例如:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
path(<b>'market/'</b>, include('marketability.mkbl_urls')),
]
否则不可能有一个名为admin
的类别。