Flask的视图函数return返回的是什么呢
1. 当我们返回一个HTML标签
@app.route('/')
def learn_response():
return '<html></html>'
页面并没有显示任何文本
image.png
2. 如何在页面中显示HTML的标签
from flask import Flask, make_response
app = Flask(__name__)
@app.route('/')
def learn_response():
headers={
'content-type':'text/plain'
#json格式
#'content-type' : 'application/json'
}
response = make_response('<html></html>',404)
response.headers = headers
return response
显示网页标签
显示网页标签
当从视图函数里面返回一个很简单的字符串时,Flask内部会将返回的主体内容以及其他信息封装成一个Response对象,处理之后再返回。那其他信息主要包括的 status code 以及放在 http headers 里的content-type,它告诉http请求方如何返回需要解析的主体内容。上述代码中,状态码设置了404,但是它只是个标识,不会影响返回的内容。
3. 设置一个location重定向到其他页面
@app.route('/')
def learn_response():
headers = {
'content-type': 'text/plain',
'location': 'http://www.zhihu.com'
# json格式
# 'content-type' : 'application/json'
}
return '<html></html>', 301, headers
重定向
重定向