视图本质上是返回一个response对象(也可以直接创建Response对象),但是在return
后不方便设置头信息等,所以还是先封装成response
对象,再返回比较全面,如下:
def index():
return render_template('index.html')
make_response可以设置返回的其他信息
def index():
response = make_response(render_template('index.html'))
response.headers['X-Parachutes'] = 'parachutes are cool'
return response
参数 | 描述 |
---|---|
str | 通过字符串创建response对象 |
bytes | 通过字节创建response对象 |
dict | 通过字典撞见response对象 |
tuple | (body, status, headers) (body, status) (body, headers) |
在使用tuple
时:
body
可以是str,bytes或dict
status
可以是str或者integer
headers
可以是dict或[(key,value),(key,value)]
如果body是一个response对象,其status会覆盖headers的
参考:
https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response
https://www.cnblogs.com/zhuchunyu/p/10466509.html