后端开发——处理响应

一、返回文本

@app.route("/text")
def ret_text():
    return "文本信息"

二、返回元组

  • (response, status)
@app.route("/tuple")
def ret_tuple():
    return "蒙德", 200
  • (response, status)
@app.route("/tuple")
def ret_tuple():
    return "蒙德", {"school": "Hogwarts"}
  • (response, status, headers)
@app.route("/tuple")
def ret_tuple():
    return "蒙德", 200, {"school": "Hogwarts"}

三、返回json格式

@app.route("/dict")
def ret_dict():
    return {"status": 0, "name": "hogwarts"}
@app.route("/json")
def ret_json():
    msg = {"status": 0, "name": "hogwarts"}
    ssg = jsonify(msg)
    return ssg

四、返回html

@app.route("/html")
def ret_html():
    return render_template("./霍格沃兹测试开发学社.html")

五、其他数据

设置额外数据

  • 添加更多的响应信息
    • 设置cookie
    • 设置响应头信息等
@app.route("/index")
def index():
    resp = make_response("霍格沃兹测试开发学社.html")
    resp.set_cookie("RSESSION", "CSRF_TOKEN")
    resp.headers["teacher"] = "ad"
    return resp