自定义Django 404错误 [英] Custom Django 404 error

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

问题描述

我有一个404.html页面,但是在某些情况下,我希望能够发送json错误消息(适用于404和500等)。我阅读了以下页面:

I have a 404.html page, but in some cases I want to be able to send a json error message (for 404 and 500, etc.). I read the following page:

https://docs.djangoproject.com/en/dev/topics/http/views/#the-404-page-not-found-view

是否有某种示例可以显示实现?我将其保存在urls.py中,但是在发生错误的情况下不会将其获取。

Is there any sort of example that shows the implementation? I have it in my urls.py but it's not being picked up in the event of an error.

推荐答案

from django.conf.urls import patterns, include, url
from django.views.static import * 
from django.conf import settings
from django.conf.urls.defaults import handler404, handler500
from app.views import error

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'app.views.home', name='home'),
)

handler404 = error.error_handler
handler500 = error.error_handler

您可以使其随心所欲转到该控制器时。

You can make it do anything as you wish when going to that controller.

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

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