对endpoint的理解
观察以下:print(app.url_map) 的输出可知,url_map输出的是url与endpoint的映射
可以理解为每个视图函数都有一个endpoint, 通过端点找到具体调用的是哪个试图函数
from flask import Flaskapp = Flask(__name__)@app.route('/test', endpoint='Test')def test():pass@app.route('/', endpoint='index')def hello_world():return 'Hello World!'if __name__ == '__main__':print(app.url_map)app.run()"""Map([<Rule '/test2' (OPTIONS, GET, HEAD) -> test2>,<Rule '/test' (OPTIONS, GET, HEAD) -> Test>,<Rule '/' (OPTIONS, GET, HEAD) -> index>,<Rule '/static/<filename>' (OPTIONS, GET, HEAD) -> static>])* Serving Flask app 'endpoint' (lazy loading)* Environment: production* Serving Flask app 'endpoint' (lazy loading)* Environment: productionWARNING: This is a development server. Do not use it in a production deployment.Use a production WSGI server instead.* Debug mode: off* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)"""
对蓝图的理解
对命名空间的理解
url_for
url_for()函数最简单的就是以视图函数名作为函数,返回对应的URL
源码注释该函数用法为:使用提供的方法生成给定端点的URL
@api.route("/hello", "/world", endpoint='mutil_endpoint')class MonogoTest(Resource):@api.doc("这是一个多url的")def get(self):"""多url接口"""return "hello world!"如上调用url_for("mutil_endpoint"), 得到的结果就是"/hello"或者"/world"
⚠️tips:当使用蓝图时,使用url_for()函数需要指定具体使用哪个蓝图下的endpoint
eg:# blueprint = Blueprint("api", __name__, url_prefix="/api/v1")url_for("blueprint.mutil_endpoint")
