日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python学习笔记-flask学习(一)route适配器

發布時間:2025/4/9 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python学习笔记-flask学习(一)route适配器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下是一個簡單的能運行的flask示例代碼,從該示例代碼中分析Flask源碼完成了哪些工作。

flask示例代碼如下:

from flask import Flaskapp = Flask(__name__)@app.route('/') def hello_world():return 'Hello World!'@app.route('/user/<name>') def user(name):return '<h1>Hello,%s!<h1>'%nameif __name__ == '__main__':app.run(debug=True)

首先調用app = Flask(__name__)構建一個Flask實例。Flask類定義在app.py文件中

"""The flask object implements a WSGI application and acts as the centralobject. It is passed the name of the module or package of theapplication. Once it is created it will act as a central registry forthe view functions, the URL rules, template configuration and much more.The name of the package is used to resolve resources from inside thepackage or the folder the module is contained in depending on if thepackage parameter resolves to an actual python package (a folder withan :file:`__init__.py` file inside) or a standard module (just a ``.py`` file).

接下來分析app.route函數完成的工作

app.route是python的一個裝飾器具體的函數代碼如下:

def route(self, rule, **options):def decorator(f):endpoint = options.pop('endpoint', None)self.add_url_rule(rule, endpoint, f, **options)return freturn decorator

在這里主要是通過調用add_url_rule函數將app實例與對應的視圖函數關聯起來

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'] = endpointmethods = options.pop('methods', None)# if the methods are not given and the view_func object knows its# methods we can use that instead. If neither exists, we go with# a tuple of only ``GET`` as default.if methods is None: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)# Methods that should always be addedrequired_methods = set(getattr(view_func, 'required_methods', ()))# starting with Flask 0.8 the view_func object can disable and# force-enable the automatic options handling.provide_automatic_options = getattr(view_func,'provide_automatic_options', None)if provide_automatic_options is None:if 'OPTIONS' not in methods:provide_automatic_options = Truerequired_methods.add('OPTIONS')else:provide_automatic_options = False# Add the required methods now.methods |= required_methodsrule = self.url_rule_class(rule, methods=methods, **options)rule.provide_automatic_options = provide_automatic_optionsself.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 如果沒有指定endpoint函數_endpoint_from_view_func通過功能函數名指定為endpoint。接下來獲取指定methods。url_rule_class(rule, methods=methods, **options)生成一rule的實例。接著url_map.add(rule)將rule與flask實例關聯起來。在前面flask實例定義了Map對象的實例。
add函數的源碼如下: def add(self, rulefactory):for rule in rulefactory.get_rules(self):rule.bind(self)self._rules.append(rule)self._rules_by_endpoint.setdefault(rule.endpoint, []).append(rule)self._remap = True

轉載于:https://www.cnblogs.com/billhust/p/6684911.html

總結

以上是生活随笔為你收集整理的python学习笔记-flask学习(一)route适配器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。