Flask路由系统

通常有3种定义路由函数的方法:

  1. 使用flask.Flask.route() 修饰器。
  2. 使用flask.Flask.add_url_rule()函数。
  3. 直接访问基于werkzeug路由系统的flask.Flask.url_map.

Part 1


让我们从最常用的@app.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函数的包装,当我们写如下代码时:

    @app.route('/index.html')
    def index():
        return "Hello World!"

实际上上面的代码转换成:

def index():
    return "Hello World!"
index = app.route('/index.html')(index)

也就是,rule = '/index.html', options = { }, 执行decorator(index) 时会执行self.add_url_rule(rule, endpoint, f, **options):

    @setupmethod
    def add_url_rule(self, rule, endpoint=None, view_func=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:
            # View Function Options (视图函数选项)
            methods = getattr(view_func, 'methods', None) or ('GET',)
        if isinstance(methods, string_types):
            raise TypeError('Allowed methods have to be iterables of strings, '
                            'for example: @app.route(..., methods=["POST"])')
        methods = set(item.upper() for item in methods)

        # View Function Options (视图函数选项)
        required_methods = set(getattr(view_func, 'required_methods', ()))

        # View Function Options (视图函数选项)
        provide_automatic_options = getattr(view_func,
            'provide_automatic_options', None)
        # View Function Options (视图函数选项)
        if provide_automatic_options is None:
            if 'OPTIONS' not in methods:
                provide_automatic_options = True
                required_methods.add('OPTIONS')
            else:
                provide_automatic_options = False

        # View Function Options (视图函数选项)
        methods |= required_methods
        
        # url_rule_class默认为Werkzeug的Rule类,
        rule = self.url_rule_class(rule, methods=methods, **options)
        rule.provide_automatic_options = provide_automatic_options

        self.url_map.add(rule)
        # view_func 不能重复定义 
        if view_func is not None:
            old_func = self.view_functions.get(endpoint)
            if old_func is not None and old_func != view_func:
                raise AssertionError('View function mapping is overwriting an '
                                     'existing endpoint function: %s' % endpoint)
            self.view_functions[endpoint] = view_func

如果endpoint参数为None,那么:

def _endpoint_from_view_func(view_func):
    """Internal helper that returns the default endpoint for a given
    function.  This always is the function name.
    """
    assert view_func is not None, 'expected view func if endpoint ' \
                                  'is not provided.'
    return view_func.__name__

endpoint就设置为view_func.name视图函数的名字。然后将endpoint添加到options字典中,对于methods = options.pop('methods', None),当我们指定时,@app.route('/login', methods=['GET', 'POST']),methods = ['GET', 'POST'] 否则methods = None. 如果methods == None, 同时,view_func 没有methods属性,则methods默认设置为('GET', ). 当然,methods不能设置为字符串类型,‘POST’可以不区分大小写。

关于View Function Options的代码暂时忽略。
add_url_rule执行完毕后,我们获得了Flask.url_map, 以及填充了Flask.view_functions.
我们可以做实验看看url_map里面都有啥,详见示例代码

Part 2


下面回过头,来看看当Flask运行时,一个Request来了,会发生什么,仍然从Flask.wsgi_app开始阅读。
已经知道,当一个Request到来时,会首先push RequestContext和AppContext,在RequestContext中的init函数中有:

...
self.url_adapter = app.create_url_adapter(self.request)
...
self.match_request()
    def create_url_adapter(self, request):
        if request is not None:
            return self.url_map.bind_to_environ(request.environ,
                server_name=self.config['SERVER_NAME'])
        ...

首先将Flask.url_map与当前到来的Request中environ进行绑定,获得一个url_adapter。

    def match_request(self):
        try:
            url_rule, self.request.view_args = \
                self.url_adapter.match(return_rule=True)
            self.request.url_rule = url_rule
        except HTTPException as e:
            self.request.routing_exception = e

获得url_adaptor之后,调用match_request,url_adapter.match()会返回一个元组view_args就是url_rule中的参数,比如Rule(/<int:year>/, endpoint='blog/archive')这个Rule,而请求是/2016/,那么view_args={year: 2016}. url_rule和view_args被储存在Request中。在Request类中,我们可以直接Request.endpoint将返回url_rule.endpoint.

在url_rule和view_args被装载到Request中后,我们继续对wsgi_app中的response = self.full_dispatch_request()这个过程与路由相关的内容进行分析。

    def full_dispatch_request(self):
        self.try_trigger_before_first_request_functions()
        try:
            request_started.send(self)
            rv = self.preprocess_request()
            if rv is None:
                rv = self.dispatch_request()
         ...

在preprocess_request()中处理与蓝本和@before_request有关的东西.暂时忽略。

    def dispatch_request(self):
        # 从_request_ctx_stack获得当前request.
        req = _request_ctx_stack.top.request
        # 如果有异常,处理异常
        if req.routing_exception is not None:
            self.raise_routing_exception(req)
        # 获得储存在request中的url_rule.
        rule = req.url_rule
        # 视图函数选项(暂时忽略)
        if getattr(rule, 'provide_automatic_options', False) \
           and req.method == 'OPTIONS':
            return self.make_default_options_response()
        # 如果没有设定视图函数选项,直接调用视图函数,在view_functions中查找
        # 键值为rule.endpoint的函数,并传入req.view_args(字典)作为
        # key-word参数。
        return self.view_functions[rule.endpoint](**req.view_args)

dispatch_request()处理完毕,将返回值储存在rv变量中。通常,视图函数会return render_template(...). 返回值接下来经过一系列处理,发送到客户端。

Part 3


视图函数选项,蓝本?(to be contiued...)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

  • 22年12月更新:个人网站关停,如果仍旧对旧教程有兴趣参考 Github 的markdown内容[https://...
    tangyefei阅读 35,127评论 22 257
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • [TOC]一直想做源码阅读这件事,总感觉难度太高时间太少,可望不可见。最近正好时间充裕,决定试试做一下,并记录一下...
    何柯君阅读 7,032评论 3 98
  • 本文大致梳理一下Flask框架在处理url路由时的主要过程。 类图 route装饰器 在Flask应用中,我们一般...
    Jakiro阅读 3,388评论 0 6
  • 在flask框架中,我们经常会遇到endpoint这个东西,最开始也没法理解这个到底是做什么的。最近正好在研究Fl...
    卡萨诺瓦_阅读 912评论 0 0