from flask import Flask
app = Flask(__name__,static_url_path='/xx')
@app.route('/index')
def index():
return 'hello world'
先执行route函数
def route(self, rule, **options):
def decorator(f):
endpoint = options.pop("endpoint", None)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator
执行add_url_rule
函数
def add_url_rule(
self,
rule,
endpoint=None,
view_func=None,
provide_automatic_options=None,
**options
):
if endpoint is None:
endpoint = _endpoint_from_view_func(view_func)
options["endpoint"] = endpoint
methods = options.pop("methods", None)
if methods is None:
methods = getattr(view_func, "methods", None) or ("GET",)
rule = self.url_rule_class(rule, methods=methods, **options)
self.url_map.add(rule)
if view_func is not None:
self.view_functions[endpoint] = view_func
url = /index
和 methods = [GET,POST]
和endpoint = "index"
封装到Rule对象app.url_map
中。app.view_functions
中。原文:https://www.cnblogs.com/liubing8/p/11930236.html