I'm using Django 2.1.3 in python 3.6.7. assume that I have this URL path:
path('admin/', admin.site.urls)
If I navigate to /ad
while the DEBUG = True
, I see normal Django 404 error page:
But if I make DEBUG = False
then the server shows up the 500.htm to me instead of 404.html for invalid URL /ad
(that matches none of the URL patterns).
The 404.html is showing up for a valid URL that raises 404 error. (like when a not-existing object query occurs with get_object_or_404
function)
here is my /templates
directory structure:
/templates
400.html
403.html
404.html
500.html
index.html
...
So, How should I tell Django to show 404 error in production (in addition to development) if the request URL matches none of the URL patterns?
Note:
- According to docs, if I have a
404.html
in the root templates directory, this 404.html will be used with the default error handler.
Update:
I figured out that it's happed because that the defaults.page_not_found
raises a Resolver404
, and that's caused by :
the path passed to resolve() doesn’t map to a view
which is the exactly what happened,(the /ad
don't match any view)
here is my urls.py:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)