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

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

问题描述

有没有办法从某个属性返回一个响应(来自make_response()对象或类似的),这样它就不会再次渲染页面,也不会做任何事情。我试图在服务器上运行一个代码,而不产生任何输出



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

< pre $ ValueError:View函数没有返回响应

应该是可能的,因为下面只是下载一个文件,并不呈现模板:

  myString =文档的第一行
response = make_response(myString)
response.headers [Content-Disposition] =attachment; filename = myFile.txt
返回响应

p>您正在响应请求,您的HTTP服务器必须返回 。 HTTP的空响应响应是 204 No Content

  return ('',204)

请注意,将文件返回给浏览器不是 em>一个空的响应,与HTML响应不同。


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

Any help will be greatly appreciated!

解决方案

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

return ('', 204)

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

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

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