...
 
Commits (2)
    https://gitcode.net/WyhPro/wyh_web_tools/-/commit/9d867b2e75262f28620db48882af8e4d3d75da33 使用render_template方法渲染模板并返回 2021-08-15T13:59:17+08:00 汪云辉 15655267350@163.com https://gitcode.net/WyhPro/wyh_web_tools/-/commit/fa70e2755be179be7f9d0e337f9ab8f1d60ca35d 自定义响应对象 2021-08-15T14:01:08+08:00 汪云辉 15655267350@163.com
......@@ -142,8 +142,6 @@ def user_info03(mob_num):
"""自定义响应对象"""
@app.route('/demo2')
def demo2():
# 视图函数可以返回str/bytes, 并且都会最终包装为Response响应对象
......
from flask import url_for, render_template
from flask import url_for, render_template, make_response
from flask import request
from user import user_blu
......@@ -32,8 +32,21 @@ def upload_file():
f.save('./demo.png')
return 'ok'
#####################################如何在不同的场景里返回不同的响应信息##########################################
# 1)返回模板
# 1)自定义响应对象
@user_blu.route('/demo2')
def demo2():
# 视图函数可以返回str/bytes, 并且都会最终包装为Response响应对象
# 手动创建响应对象 主要目的为设置响应头
response = make_response("demo_test") # type: Response
response.headers['a'] = 10
return response
# 2)返回模板
# 使用render_template方法渲染模板并返回
# 例如,新建一个模板index.html
......@@ -42,5 +55,3 @@ def index001():
mstr = 'Hello 黑马程序员'
mint = 10
return render_template('index.html', my_str=mstr, my_int=mint)