Python 如何在没有模板的情况下在 Django 中发送空响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4123155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:21:50  来源:igfitidea点击:

How do I send empty response in Django without templates

pythonajaxdjangodjango-views

提问by Srikar Appalaraju

I have written a view which responds to ajax requests from browser. It's written like so -

我写了一个响应来自浏览器的 ajax 请求的视图。是这么写的——

@login_required
def no_response(request):
    params = request.has_key("params")
    if params:
        # do processing
        var = RequestContext(request, {vars})
        return render_to_response('some_template.html', var)
    else: #some error
        # I want to send an empty string so that the 
        # client-side javascript can display some error string. 
        return render_to_response("") #this throws an error without a template.

How do i do it?

我该怎么做?

Here's how I handle the server response on client-side -

这是我在客户端处理服务器响应的方式 -

    $.ajax
    ({
        type     : "GET",
        url      : url_sr,
        dataType : "html",
        cache    : false,
        success  : function(response)
        {
            if(response)
                $("#resp").html(response);
            else
                $("#resp").html("<div id='no'>No data</div>");
        }
    });

采纳答案by Daniel Roseman

render_to_responseis a shortcut specifically for rendering a template. If you don't want to do that, just return an empty HttpResponse:

render_to_response是专门用于渲染模板的快捷方式。如果您不想这样做,只需返回一个空的HttpResponse

 from django.http import HttpResponse
 return HttpResponse('')

However, in this circumstance I wouldn't do that - you're signalling to the AJAX that there was an error, so you should return an error response, possibly code 400 - which you can do by using HttpResponseBadRequestinstead.

但是,在这种情况下,我不会这样做 - 您正在向 AJAX 发出错误信号,因此您应该返回错误响应,可能是代码 400 - 您可以使用它HttpResponseBadRequest来代替。

回答by robermorales

I think the best code to return an empty response is 204 No Content.

我认为返回空响应的最佳代码是204 No Content.

from django.http import HttpResponse
return HttpResponse(status=204)

However, in your case, you should not return an empty response, since 204 means: The server *successfully* processed the request and is not returning any content..

然而,在你的情况,你不应该返回一个空的响应,因为204级是指:The server *successfully* processed the request and is not returning any content.

It is better returning some 4xxstatus code to better signal the error is in the client side. Yo can put any string in the body of the 4xxresponse, but I highly recommend you send a JSONResponse:

最好返回一些4xx状态代码以更好地表明错误发生在客户端。您可以在4xx响应正文中放置任何字符串,但我强烈建议您发送JSONResponse

from django.http import JsonResponse
return JsonResponse({'error':'something bad'},status=400)