Django,创建自定义 500/404 错误页面 [英] Django, creating a custom 500/404 error page

查看:34
本文介绍了Django,创建自定义 500/404 错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照教程找到此处,我无法创建自定义 500或 404 错误页面.如果我输入了错误的 url,该页面会给我默认的错误页面.有什么我应该检查的东西会阻止自定义页面显示吗?

Following the tutorial found here exactly, I cannot create a custom 500 or 404 error page. If I do type in a bad url, the page gives me the default error page. Is there anything I should be checking for that would prevent a custom page from showing up?

文件目录:

mysite/
    mysite/
        __init__.py
        __init__.pyc
        settings.py
        settings.pyc
        urls.py
        urls.pyc
        wsgi.py
        wsgi.pyc
    polls/
        templates/
            admin/
                base_site.html
            404.html
            500.html
            polls/
                detail.html
                index.html
        __init__.py
        __init__.pyc
        admin.py
        admin.pyc
        models.py
        models.pyc
        tests.py
        urls.py
        urls.pyc
        view.py
        views.pyc
    templates/
    manage.py

在 mysite/settings.py 中,我启用了这些:

within mysite/settings.py I have these enabled:

DEBUG = False
TEMPLATE_DEBUG = DEBUG

#....

TEMPLATE_DIRS = (
    'C:/Users/Me/Django/mysite/templates', 
)

在 mysite/polls/urls.py 中:

within mysite/polls/urls.py:

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<poll_id>d+)/$', views.detail, name='detail'),
    url(r'^(?P<poll_id>d+)/results/$', views.results, name='results'),
    url(r'^(?P<poll_id>d+)/vote/$', views.vote, name='vote'),
)

我可以发布任何其他必要的代码,但是如果我使用了错误的 url,我应该更改什么以获得自定义的 500 错误页面?

I can post any other code necessary, but what should I be changing to get a custom 500 error page if I use a bad url?

编辑

解决方案:我有一个额外的

TEMPLATE_DIRS

在我的 settings.py 中,这导致了问题

within my settings.py and that was causing the problem

推荐答案

在你的主views.py下添加你自己的自定义实现下面两个视图,只需要设置模板404.html500.html 以及您想要显示的内容.

Under your main views.py add your own custom implementation of the following two views, and just set up the templates 404.html and 500.html with what you want to display.

使用此解决方案,无需将自定义代码添加到 urls.py

With this solution, no custom code needs to be added to urls.py

代码如下:

from django.shortcuts import render_to_response
from django.template import RequestContext


def handler404(request, *args, **argv):
    response = render_to_response('404.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 404
    return response


def handler500(request, *args, **argv):
    response = render_to_response('500.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response

更新

handler404handler500 是在 django/conf/urls/__init__.py 中找到的导出 Django 字符串配置变量.这就是上述配置有效的原因.

handler404 and handler500 are exported Django string configuration variables found in django/conf/urls/__init__.py. That is why the above config works.

要使上述配置工作,您应该在 urls.py 文件中定义以下变量,并将导出的 Django 变量指向定义这些 Django 功能视图的字符串 Python 路径,像这样:

To get the above config to work, you should define the following variables in your urls.py file and point the exported Django variables to the string Python path of where these Django functional views are defined, like so:

# project/urls.py

handler404 = 'my_app.views.handler404'
handler500 = 'my_app.views.handler500'

Django 2.0 的更新

处理程序视图的签名在 Django 2.0 中发生了变化:https://docs.djangoproject.com/en/2.0/ref/视图/#error-views

Signatures for handler views were changed in Django 2.0: https://docs.djangoproject.com/en/2.0/ref/views/#error-views

如果您使用上述视图,handler404 将失败并显示消息:

If you use views as above, handler404 will fail with message:

handler404() 得到了一个意外的关键字参数‘异常’"

"handler404() got an unexpected keyword argument 'exception'"

在这种情况下,像这样修改您的视图:

In such case modify your views like this:

def handler404(request, exception, template_name="404.html"):
    response = render_to_response(template_name)
    response.status_code = 404
    return response

这篇关于Django,创建自定义 500/404 错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆