1 提出问题
如何实现前端传过去的路径时动态的(即:多个url对应一个url视图函数)
例如:
浏览器中输入 http://127.0.0.1:5000/test/good/ 或者 http://127.0.0.1:5000/test/fury/ 时,在后台执行的都是同一个url视图函数
2 解决问题
利用flask的转换器实现
2.1 什么是转换器
@app.route('/test/<string:let>/', endpoint="test01") 中 <> 的部分就叫做转换器
代码解释:路径的最后一部分可以是任意值,但是这个值得类型必须是string类型
from flask import Flask app = Flask(__name__) @app.route("/") def index(): return "这是主页面, hello" @app.route('/test/<string:let>/', endpoint="test01") def test(let): return "多个url对应一个url视图函数 " + let print(app.url_map) if __name__ == "__main__": app.run(debug=True)