7

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:

enter image description here

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)
M.A. Heshmat Khah
  • 740
  • 12
  • 27

2 Answers2

4

I figured out that one of my custom template context processors uses django.urls.resolve and as the path don't match any view, the resolve raises Resolver404.

So I fix that context processor and the problem solved!

M.A. Heshmat Khah
  • 740
  • 12
  • 27
-1

You need to make a 404 view that will render when no URL match. Then you need to set it hander404 in urls.py

handler404 = 'mysite.views.my_custom_page_not_found_view'

For more information: https://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views

shafik
  • 6,098
  • 5
  • 32
  • 50