0

I am attempting to serve custom error pages in my Django application running under NGINX. I have URLs defined for four pages, and navigating to them is successful; it shows the appropriate page with the appropriate status code. The trouble arises, however, when I try to produce an error via normal means. When I try to access a restricted file, for example, I see the proper status code (403) in Firefox's network panel, but I see not NGINX's default error page or my custom page, but a blank white page.

Here's my NGINX (v1.14.2) configuration:

server {
    listen 80;
    server_name my_IP;

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    error_page 400 /400;
    error_page 403 /403;
    error_page 404 /404;
    error_page 500 /500;

    location = /favicon.ico {
        root /home/matt/my-project/staticfiles/home/img;
        #access_log off;
        #log_not_found off;
    }

    location /static {
        alias /home/matt/my-project/staticfiles;
    }

    location /media {
        root /home/matt/my-project;
    }
}

And my Django (v3.0.5) source:

project/urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import (
    handler400,
    handler403,
    handler404,
    handler500,
)
from django.conf.urls.static import static

from mtm.settings import DEBUG, MEDIA_URL, MEDIA_ROOT

urlpatterns = [
    path('', include(('home.urls', 'home'), namespace='home')),
    path('', include(('users.urls', 'users'), namespace='users')),
    path('', include(('locations.urls', 'locations'), namespace='locations')),
    path('', include(('events.urls', 'events'), namespace='events')),
    path('images/', include(('images.urls', 'images'), namespace='images')),
    path('articles/', include(('articles.urls', 'articles'), namespace='articles')),
    path('admin/', admin.site.urls),
]

if DEBUG:
    urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)

handler400 = 'home.views.handler400'
handler403 = 'home.views.handler403'
handler404 = 'home.views.handler404'
handler500 = 'home.views.handler500'

home/urls.py

from django.urls import path, re_path

from . import views
from mtm.settings import DEBUG

urlpatterns = [
    path('', views.index, name='index'),
    re_path(r'^400/', views.handler400),
    re_path(r'^403/', views.handler403),
    re_path(r'^404/', views.handler404),
    re_path(r'^500/', views.handler500),
    path('about/', views.about, name='about'),
    path('people-to-know/', views.people, name='people-to-know'),
    path('create-person/', views.create_person, name='create-person'),
    re_path(r'update-person/(?P<person_id>[1-9]\d*)/', views.update_person, name='update-person'),
    re_path(r'delete-person/(?P<person_id>[1-9]\d*)/', views.delete_person, name='delete-person'),
    path('news-feed/', views.news_feed, name='news-feed'),
    re_path(r'^(?P<slug>(nightlife|restaurants|nightlife-restaurants|arts-and-entertainment|health-and-fitness|sports|non-profit))/$', views.category, name='category'),
]

home/views.py

# imports

def handler400(request, exception=None):
    return render(request, 'home/errors/400.html', status=400)

def handler403(request, exception=None):
    return render(request, 'home/errors/403.html', status=403)

def handler404(request, exception=None):
    return render(request, 'home/errors/404.html', status=404)

def handler500(request, exception=None):
    return render(request, 'home/errors/500.html', status=500)

# other views

Any help is appreciated!

1 Answers1

0

A callable, or a string representing the full Python import path to the view that should be called if the HTTP client has sent a request that caused an error condition and a response with a status code of 400.

There is still other option for setting error handler function, i.e, callable.

django's default error handler functions

django.conf.urls

BTW, Pasting the curl -vvv your-site.com/404 command output out here would be better.

Kanaza
  • 13
  • 2