NGINX/Gunicorn/Django 不提供自定义错误页面

NGINX/Gunicorn/Django 不提供自定义错误页面

我正在尝试在 NGINX 下运行的 Django 应用程序中提供自定义错误页面。我为四个页面定义了 URL,导航到这些页面是成功的;它显示了带有相应状态代码的相应页面。然而,当我尝试通过正常方式产生错误时,问题就出现了。例如,当我尝试访问受限制的文件时,我在 Firefox 的网络面板中看到了正确的状态代码 (403),但我看到的不是 NGINX 的默认错误页面或我的自定义页面,而是一个空白页。

这是我的 NGINX(v1.14.2)配置:

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;
    }
}

我的 Django (v3.0.5) 源代码:

项目/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'

主页/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'),
]

主页/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

任何帮助都将不胜感激!

答案1

一个可调用函数或一个字符串,表示视图的完整 Python 导入路径,如果 HTTP 客户端发送了导致错误情况的请求以及状态代码为 400 的响应,则应该调用该函数。

还有其他选项可用于设置错误处理函数,即可调用。

django 的默认错误处理函数

django.conf.urls

顺便说一句,将curl -vvv your-site.com/404命令输出粘贴到这里会更好。

相关内容