亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

從app.route裝飾器引發(fā)對endpoint的思考

妤鋒シ / 2099人閱讀

摘要:為字段賦值,返回給客戶端進(jìn)行重定向總結(jié)一個(gè)視圖函數(shù)的如果不設(shè)置那么就是視圖函數(shù)名。為和搭起橋梁,使得整個(gè)后端框架更加靈活。返回的是視圖函數(shù)對應(yīng)的,是對應(yīng)視圖函數(shù)裝飾器傳入的值。

還是先來看看源碼

def route(self, rule, **options):
    """A decorator that is used to register a view function for a
    given URL rule.  This does the same thing as :meth:`add_url_rule`
    but is intended for decorator usage::

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

    For more information refer to :ref:`url-route-registrations`.

    :param rule: the URL rule as string
    :param endpoint: the endpoint for the registered URL rule.  Flask
                     itself assumes the name of the view function as
                     endpoint
    :param options: the options to be forwarded to the underlying
                    :class:`~werkzeug.routing.Rule` object.  A change
                    to Werkzeug is handling of method options.  methods
                    is a list of methods this rule should be limited
                    to (``GET``, ``POST`` etc.).  By default a rule
                    just listens for ``GET`` (and implicitly ``HEAD``).
                    Starting with Flask 0.6, ``OPTIONS`` is implicitly
                    added and handled by the standard request handling.
    """
    def decorator(f):
        endpoint = options.pop("endpoint", None)
        self.add_url_rule(rule, endpoint, f, **options)
        return f
    return decorator

route傳入了**options這樣一個(gè)字典,一般我們會傳方法methods進(jìn)去,GET、POST,如果不自己設(shè)置endpoint=....的話默認(rèn)就是No。
然后進(jìn)入add_url_rule函數(shù)看一看:

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)

...
...
...

    self.url_map.add(rule)
    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

這里我截取了一些重點(diǎn)的,可以看到如果endpoint為None,會調(diào)用_endpoint_from_view_func函數(shù)來給endpoint賦值,
看一下_endpoint_from_view_func的代碼:

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__

可以看到將視圖函數(shù)名賦值給了endpoint,所以如果我們創(chuàng)建視圖函數(shù)時(shí)不在**options中指明endpoint的話,默認(rèn)就是視圖函數(shù)名,
后半部分進(jìn)行了判斷,保證了endpoint的唯一,并將view_func保存在view_functions這個(gè)字典中,并且和endpoint形成映射關(guān)系,還將路徑加入到當(dāng)前應(yīng)用中, 這樣做的好處是,當(dāng)我們用url_for()從一個(gè)endpoint來找到一個(gè)URL時(shí),可以順著這個(gè)映射關(guān)系來找,而不用寫URL, 常見的用法是url_for(blueprint.endpoint,parm=val...)進(jìn)行重定向,這樣可以獲取一個(gè)視圖函數(shù)的路徑,傳給redirect(),
redirect(location,code=302)函數(shù)會把接收的參數(shù)作為響應(yīng)body中的Location字段返回給客戶端,并且默認(rèn)是302臨時(shí)重定向。

def redirect(location, code=302, Response=None):
...
...

#為Location字段賦值,返回給客戶端進(jìn)行重定向
response.headers["Location"] = location
        return response

總結(jié):
URL《————》endpoint《————》view
一個(gè)視圖函數(shù)的endpoint如果不設(shè)置那么就是視圖函數(shù)名。
為URL和view搭起橋梁,使得整個(gè)后端框架更加靈活。
url_for(.endpoint)返回的是視圖函數(shù)對應(yīng)的URL,URL是對應(yīng)視圖函數(shù)裝飾器傳入的值。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/44735.html

相關(guān)文章

  • flask route設(shè)計(jì)思路

    摘要:引言本文主要梳理了源碼中的設(shè)計(jì)思路。協(xié)議將處理請求的組件按照功能及調(diào)用關(guān)系分成了三種。不論是什么,最終都會調(diào)用函數(shù)。 引言 本文主要梳理了flask源碼中route的設(shè)計(jì)思路。首先,從WSGI協(xié)議的角度介紹flask route的作用;其次,詳細(xì)講解如何借助werkzeug庫的Map、Rule實(shí)現(xiàn)route;最后,梳理了一次完整的http請求中route的完整流程。 flask rou...

    vvpale 評論0 收藏0
  • Python裝飾-裝飾流程,執(zhí)行順序

    摘要:最近看到一個(gè)關(guān)于的題文章其中的一個(gè)是裝飾器的順序問題就想寫篇博客回顧下裝飾器首先強(qiáng)烈推薦很久之前看的一篇博文翻譯理解中的裝飾器關(guān)于什么是裝飾器看這篇文章就好了這里主要想寫關(guān)于多個(gè)裝飾器的執(zhí)行流程裝飾順序示例代碼初始化初始化輸出結(jié)果初始化初始 最近看到一個(gè)關(guān)于Flask的CTF(RealWorld CTF 2018 web題bookhub)文章其中的一個(gè)trick是裝飾器的順序問題,就想...

    cpupro 評論0 收藏0
  • Flask注冊視圖函數(shù)

    摘要:鍵是函數(shù)名,值是函數(shù)對象,函數(shù)名也用于生成。注冊一個(gè)視圖函數(shù),用裝飾器。獲取儲存視圖函數(shù)字典中的函數(shù)對象視圖函數(shù)類中的字典儲存了注冊的視圖函數(shù)名和視圖函數(shù)對象。輸出視圖函數(shù)視圖函數(shù)名重復(fù)修改解決 那天莫名其妙出了個(gè)錯(cuò)。。就順便看了看Flask路由 在flask存儲路由函數(shù)是以函數(shù)名為鍵,函數(shù)對象為值 class Flask: def __init__(self, *args, ...

    2bdenny 評論0 收藏0
  • flask之三:視圖高級

    摘要:視圖高級和這個(gè)方法是用來添加與視圖函數(shù)的映射。小例子如下請求上下文的定義,結(jié)合類視圖之前我們接觸的視圖都是函數(shù),所以一般簡稱視圖函數(shù)。 視圖高級 app.route和app.add_url_rule app.add_url_rule app.add_url_rule(/list/,endpoint=myweb,view_func=my_list) 這個(gè)方法是用來添加url與視圖函數(shù)...

    hot_pot_Leo 評論0 收藏0
  • flask之三:視圖高級

    摘要:視圖高級和這個(gè)方法是用來添加與視圖函數(shù)的映射。小例子如下請求上下文的定義,結(jié)合類視圖之前我們接觸的視圖都是函數(shù),所以一般簡稱視圖函數(shù)。 視圖高級 app.route和app.add_url_rule app.add_url_rule app.add_url_rule(/list/,endpoint=myweb,view_func=my_list) 這個(gè)方法是用來添加url與視圖函數(shù)...

    RancherLabs 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<