简体   繁体   English

当Debug = False时完全禁用Http 404/500模板

[英]Completely disable Http 404/500 templates when Debug=False

I'm writing some methods in Django that return json. 我在Django中写了一些返回json的方法。 I'm handling exceptions myself and in certain scenarios, I want to return an Http 500 response with a Json body so the client can extract (amongst other things) an Exception uuid. 我自己处理异常,在某些情况下,我想用Json主体返回Http 500响应,以便客户端可以提取(除其他外)一个异常uuid。

This worked fine with Debug = True but when set to False , the following code results in an html error page... 这适用于Debug = True但是当设置为False ,以下代码会导致html错误页面...

Ret = {"ExceptionId:": "<Exception_uuid>",
       "Message": "Some user-friendly error message"}

return HttpResponse(json.dumps(Ret), content_type="application/json", status=INTERNAL_SERVER_ERROR)

I'm aware of the security implications which is why the Message returned here is in no way related to the exception but is generated by our own code when an exception is raised (or a generic one provided in some circumstances). 我知道安全隐患,这就是为什么这里返回的Message与异常无关,而是在引发异常时由我们自己的代码生成(或者在某些情况下提供的通用代码)。 The uuid is logged (ideally to database but to file if that's not possible) along with stack traces, the real exception information, etc. The user can refer to the exception by uuid when contacting us. uuid被记录(理想情况下是数据库,但如果不可能则存档)以及堆栈跟踪,真实异常信息等。用户可以在联系我们时通过uuid引用异常。

How can I force django not to try and interfere with a fuzzy html page that I don't want or need? 我如何强制django 不要试图干扰我不想要或不需要的模糊html页面?

I want to return an Http 500 response with a Json body so the client can extract (amongst other things) an Exception uuid. 我想用Json主体返回Http 500响应,以便客户端可以提取(除其他外)一个异常uuid。

TBH, I wouldn't bank on all web browsers treating non-200 responses as valid XHR responses, so it's probably safer to always return a 200 response when you plan to return a JSON response that you want the browser to parse. TBH,我不会将所有将非200响应视为有效XHR响应的Web浏览器,因此当您计划返回希望浏览器解析的JSON响应时,总是返回200响应可能更安全。

One common way to do this is to return the same structure for all JSON responses, such that the browser can determine if the call succeeded or not. 一种常见的方法是为所有JSON响应返回相同的结构,以便浏览器可以确定调用是否成功。 For example, you could return something like this... 例如,你可以返回这样的东西......

{"result": "Hello, world!", "exception": null}

...if it worked, and something like this... ......如果有效,还有这样的......

{"result": null, "exception": "You provided incorrect parameters"}

...if it fails. ......如果失败了 For bonus points, you could even translate the exception into a JavaScript exception, and throw it back to the calling function. 对于奖励积分,您甚至可以将异常转换为JavaScript异常,并将其抛回调用函数。

Update 更新

To simplify processing, it's a lot easier if all your AJAX calls are routed through a single function. 为简化处理,如果所有AJAX调用都通过单个函数进行路由,则会更容易。 Something like... 就像是...

function do_json_call(url)
{
    var json = null;
    // code to get JSON via XHR here
    var result = json['result'];
    var exc = json['exception'];
    if (exc !== null)
    {
        throw exc;
    }
    return result;
}

function func1()
{
    try
    {
        var result = do_json_call("http://localhost/json/func1");
        // Do something
    }
    catch (e)
    {
        alert(e);
    }
}

function func2()
{
    try
    {
        var result = do_json_call("http://localhost/json/func2");
        // Do something
    }
    catch (e)
    {
        alert(e);
    }
}

// etc..

In your ROOT_URLCONF ie urls.py add an attribute named handler500 . 在您的ROOT_URLCONF即urls.py中添加名为handler500的属性。

urls.py urls.py

....
....
handler500 = 'myapp.views.get500handler'

myapp/views.py MYAPP / views.py

def get500h(request):
    return HttpResponse("A server error ocurred")

Do not worry about what we are returning from get500h() , your user will still see the json response that you are returning. 不要担心我们从get500h()返回的get500h() ,您的用户仍会看到您要返回的json响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM