Search code examples
djangodjango-admindjango-urlsdjango-settings

Django admin/ return 404


Starting development server at http://127.0.0.1:8000/

Not Found: /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')),
    ]

variants path(r'admin/', admin.site.urls), and path(r'^admin/', admin.site.urls), don't works too.

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 has created

So, i don't see any deviations from Django Documentation. How to solve it? thks


Solution

  • Your path:

    path('<slug:cat_>/', views.page_Category_Main, name='cat'),

    will match with admin/ and thus see admin as the value for the cat_ slug, and thus trigger that view. Likely the view for that path will try to fetch an element with admin as slug, and fail to do this, and thus raise a HTTP 404 response.

    You can put the urls for the admin/ and ckeditor/ before the one with your categories:

    from django.contrib import admin
    from django.urls import include, path
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('ckeditor/', include('ckeditor_uploader.urls')),
        path('', include('marketability.mkbl_urls')),
    ]

    Django will visit the url patterns top to bottom, and thus trigger the first path pattern that matches, in that case the one with the admin.

    It might however be better to prefix your marketability urls as well, for example with:

    from django.contrib import admin
    from django.urls import include, path
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('ckeditor/', include('ckeditor_uploader.urls')),
        path('market/', include('marketability.mkbl_urls')),
    ]

    otherwise it is thus impossible to have a category named admin.