在 Django 中设置 http 错误自定义页面

Setting up http error custom pages in django

我想向我的网站添加自定义错误页面,但我得到的是简单页面,例如 403 错误我没有得到我构建的模板。模板位于 templates/http_errors/ 文件夹中。

我在 settings 文件中的模板设置如下所示:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [normpath(join(PROJECT_DIR, 'templates'))],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.media',
                'django.template.context_processors.i18n'
            ],
        },
    },
]

urls.py:

from django.conf.urls import handler403
handler403 = 'core.views.custom_handler403'

views.py:

def custom_handler403(request):
    response = render_to_response('http_errors/HTTP403.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 403
    return response

我尝试了很多在 django 文档和 Whosebug 上找到的选项,但仍然找不到答案。如果你能帮助我,那就太好了。

P.S。我正在使用 Django 1.10 和 Python 3.5

这些自定义页面只有在您的设置中有 DEBUG = False 时才有效。否则将使用正常的调试处理程序