Django Admin Page not found (404), urls.py + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Viewed 39

Going to http://127.0.0.1:8000/admin returns this error:

Using the URLconf defined in the_food_blog.urls, Django tried these URL patterns, in this order:

admin/
[name='home']
<slug:slug>/ [name='post_detail']
^(?P<path>.*)$
The current path, admin, matched the last one.

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urls.py

urlpatterns = [
    path('', views.PostList.as_view(), name='home'),
    path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
]

Removing + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) fixes this but why?

1 Answers

You can use these:

urls.py

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py

MEDIA_URL = '/media/'

I hope to solve your problem!

Related