源碼

  • 請求鉤子
    before_request after_request errorhandler
class Flask(object):
    # ...
    
    def before_request(self, f):
        """Registers a function to run before each request."""
        self.before_request_funcs.append(f)
        return f

    def after_request(self, f):
        """Register a function to be run after each request."""
        self.after_request_funcs.append(f)
        return f
    def errorhandler(self, code):
        def decorator(f):
            self.error_handlers[code] = f
            return f
        return decorator
  • 裝飾器
    route
class Flask(object)
    # ...
    def route(self, rule, **options):
        """
        By default a variable part
        in the URL accepts any string without a slash however a different
        converter can be specified as well by using ``<converter:name>``.   
        =========== ===========================================
        `int`       accepts integers
        `float`     like `int` but for floating point values
        `path`      like the default but also accepts slashes
        =========== ===========================================
        An important detail to keep in mind is how Flask deals with trailing
        slashes.  The idea is to keep each URL unique so the following rules
        apply:
        1. If a rule ends with a slash and is requested without a slash
           by the user, the user is automatically redirected to the same
           page with a trailing slash attached.
        2. If a rule does not end with a trailing slash and the user request
           the page with a trailing slash, a 404 not found is raised.
        The :meth:`route` decorator accepts a couple of other arguments
        as well:
        :param rule: the URL rule as string
        :param methods: a list of methods this rule should be limited
                        to (``GET``, ``POST`` etc.).  By default a rule
                        just listens for ``GET`` (and implicitly ``HEAD``).
        :param subdomain: specifies the rule for the subdoain in case
                          subdomain matching is in use.
        :param strict_slashes: can be used to disable the strict slashes
                               setting for this rule.  See above.
        :param options: other options to be forwarded to the underlying
                        :class:`~werkzeug.routing.Rule` object.
        """
        def decorator(f):
            self.add_url_rule(rule, f.__name__, **options)
            self.view_functions[f.__name__] = f
            return f
        return decorator

slash : /
例如

@app.route('/projects/')
def projects():
    pass

如果URL為:*/projects嫌套,則會重定向到/projects/。反之報(bào)404圾旨。

add_url_rule()

class Flask(object):
    # ...
    def add_url_rule(self, rule, endpoint, **options):
        """Connects a URL rule.  Works exactly like the :meth:`route`
        decorator but does not register the view function for the endpoint.
        Basically this example::
            @app.route('/')
            def index():
                pass
        Is equivalent to the following::
            def index():
                pass
            app.add_url_rule('index', '/')
            app.view_functions['index'] = index

        :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
        """
        options['endpoint'] = endpoint
        options.setdefault('methods', ('GET',))
        self.url_map.add(Rule(rule, **options))
  • WSGI協(xié)議
class Flask(object):
    # ...
    def __call__(self, environ, start_response):
        """Shortcut for :attr:`wsgi_app`"""
        return self.wsgi_app(environ, start_response)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末踱讨,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子砍的,更是在濱河造成了極大的恐慌痹筛,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件廓鞠,死亡現(xiàn)場離奇詭異帚稠,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)床佳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進(jìn)店門滋早,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人夕土,你說我怎么就攤上這事馆衔∥僚校” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵角溃,是天一觀的道長拷获。 經(jīng)常有香客問我,道長减细,這世上最難降的妖魔是什么匆瓜? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮未蝌,結(jié)果婚禮上驮吱,老公的妹妹穿的比我還像新娘。我一直安慰自己萧吠,他們只是感情好左冬,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著纸型,像睡著了一般拇砰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上狰腌,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天除破,我揣著相機(jī)與錄音,去河邊找鬼琼腔。 笑死瑰枫,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的丹莲。 我是一名探鬼主播光坝,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼圾笨!你這毒婦竟也來了教馆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤擂达,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后胶滋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體板鬓,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年究恤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了俭令。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,680評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡部宿,死狀恐怖抄腔,靈堂內(nèi)的尸體忽然破棺而出瓢湃,到底是詐尸還是另有隱情,我是刑警寧澤赫蛇,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布绵患,位于F島的核電站,受9級特大地震影響悟耘,放射性物質(zhì)發(fā)生泄漏落蝙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一暂幼、第九天 我趴在偏房一處隱蔽的房頂上張望筏勒。 院中可真熱鬧,春花似錦旺嬉、人聲如沸管行。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捐顷。三九已至,卻和暖如春悲酷,著一層夾襖步出監(jiān)牢的瞬間套菜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工设易, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留逗柴,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓顿肺,卻偏偏與公主長得像戏溺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子屠尊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容