flask响应response使用举例

#! /usr/bin/python3
# -*- coding:utf-8 -*-
# file: lflask.py
# author: wangchenxi
# mail: wongchenxi@icloud.com
# brief:
# version: 0.1.00
# Create Time:2019-12-23 00:45:20
# Last Update: 2019-12-23 01时03分46秒

from flask import Flask, render_template, redirect, jsonify, send_file, request
app = Flask(__name__) #配置传参后续章节再说明

@app.route("/response", methods=["get", "PoSt"],
        endpoint="my_response",strict_slashes=True)  # 默认Get 请求方式,配置传参后续章节再说明
def my_response():
    if request.method == "GET":
        return render_template("login.html") # 项目默认配置的模板目录是templates


@app.route("/response1", methods=["get", "PoSt"],
        endpoint="my_response1",strict_slashes=True)  # 默认Get 请求方式,配置传参后续章节再说明
def my_response1():
    if request.method == "GET":
        return redirect("/response")


@app.route("/response2", methods=["get", "PoSt"],
        endpoint="my_response2",strict_slashes=True)  # 默认Get 请求方式,配置传参后续章节再说明
def my_response2():
    if request.method == "GET":
        d = {
                "name": "Alexander.DSB.Li"
                }
        return jsonify(d)


@app.route("/response3", methods=["get", "PoSt"],
        endpoint="my_response3",strict_slashes=True)  # 默认Get 请求方式,配置传参后续章节再说明
def my_response3():
    if request.method == "GET":
        d = {
                "name": "Alexander.DSB.Li"
                }
        return d


@app.route("/response4", methods=["get", "PoSt"],
        endpoint="my_response4",strict_slashes=True)  # 默认Get 请求方式,配置传参后续章节再说明
def my_response4():
    if request.method == "GET":
        return send_file("2.png") #项目目录下2.png文件


@app.route("/response5", methods=["get", "PoSt"],
        endpoint="my_response5",strict_slashes=True)  # 默认Get 请求方式,配置传参后续章节再说明
def my_response5():
    if request.method == "GET":
        return 'hello,world.'


if __name__ == '__main__':
    app.run('0.0.0.0', 8000)

版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创

转载请注明:出处来自王晨曦个人站点 » flask响应response使用举例

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注