Flask框架返回值

Flask中的HTTPResponse

def index(): #视图函数
    return 'Hello World'  #直接return就是返回的字符串

Flask中的Redirect,和django一样

@qpp.route("/login")
def index():
    return redirect("/login/")

Flask 中的 render (render_template)

from flask import Flask, render_template

@app.route("/login")
def login():
    return render_template("login.html")

使用 render_template 返回渲染的模板,请在项目的主目录中加入一个目录 templates

右击templates选择Mark Directory as --> Template Folder

 yes

 jinjia2 --> OK

send_file

send_file: 打开并返回文件内容 自动识别文件类型 并且加入 Content-type:文件类型,无法识别返回text

@app.route("/mp4")
def image():
    return send_file("1.mp4")

jsonify

返回标准格式的JSON字符串 在响应头中加入 Content-type:application/json

@app.route()
def get_jsom():
    dict = {"name":"value"}
    return jsonify(dict)
原文地址:https://www.cnblogs.com/wanglan/p/10553286.html