Falsk返回参数【带参视图函数】

# 带返回参数的视图函数
# 这里的test以及test2都是带参数的视图函数

from flask import Flask

# 实例化flask类,传入必要一个参数: __name__
app = Flask(__name__)


# arg默认是str
@app.route('/test/<arg>')
# 限定arg类型为整型
# @app.route('/test/<int:arg>')
# 限定其他类型同理
# @app.route('/test/<path:arg>')    返回路径
def test(arg):
    # TODO:搞明白这里
    str_arg = type(arg).__name__
    print(str_arg)
    return '通过路由地址传递参数类型:{},类型属于:{}'.format(arg, str_arg)


# 传递多个参数
# @app.route('/test2/<name>/<sex>/<age>/')  # 第一种方式
@app.route('/test2/<name>_<sex>_<age>/')  # 第二种方式
def test2(name, sex, age):
    return '姓名:{},性别:{},年龄:{}'.format(name, sex, age)


# 添加路由
@app.route('/')
def index():
    return "hello flask"


# 添加路由
@app.route('/hello/')
def hel_index():
    return "hello flask"


if __name__ == "__main__":
    # 当flask默认启动时,是本机模式并且是开发者模式,
    # 端口默认启动是5000
    # 通过host和port进行域名和端口修改
    # debug=True 错误信息会返回到控制台和浏览器界面
    app.run(host='127.0.0.1', port=5001, debug=True)
    # print(index())
原文地址:https://www.cnblogs.com/cxstudypython/p/12399014.html