Python Flask 故意空响应 [英] Python Flask Intentional Empty Response

查看:26
本文介绍了Python Flask 故意空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法返回具有某些属性的响应(来自 make_response() 对象或类似对象),这样它就不会再次呈现页面并且不执行任何其他操作.我正在尝试在服务器上运行代码而不生成任何输出

Is there a way to return a response (from make_response() object or similar) with certain properties so that it doesn't render the page again and doesn't do anything else either. I am trying to run a code on the server without generating any output

一个简单的返回无"产生:

A simple 'return None' produces:

ValueError: View function did not return a response

这应该是可能的,因为以下仅下载文件而不呈现模板:

This should be possible because the following only downloads a file and doesn't render the template:

myString = "First line of a document"
response = make_response(myString)
response.headers["Content-Disposition"] = "attachment; filename=myFile.txt"
return response

推荐答案

你正在响应一个请求,你的 HTTP 服务器必须返回something.HTTP '空响应' 响应是 204 No Content:

You are responding to a request, your HTTP server must return something. The HTTP 'empty response' response is 204 No Content:

return ('', 204)

请注意,向浏览器返回文件不是一个空响应,只是与 HTML 响应不同.

Note that returning a file to the browser is not an empty response, just different from a HTML response.

这篇关于Python Flask 故意空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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