flask 源码专题(八):路由加载
2024-08-28 07:53:25
1.示例代码
from flask import Flask app = Flask(__name__,static_url_path='/xx') @app.route('/index')
def index():
return 'hello world
2.路由加载源码分析
2.1先执行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
2.2 执行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对象 - 将Rule对象添加到
app.url_map
中。 - 把endpoint和函数的对应关系放到
app.view_functions
中。 - 当一个请求过来时,先拿路由在app.url_map找对应的别名,再在app.view_functions中找到别名对应的视图函数
最新文章
- C# Combobox 设置 value
- Firebug在Firefox DevTools 中复活
- linux -小记(3) 问题:linux 安装epel扩展源报错
- Javascript 中的闭包和引用
- 应用程序池“Classic .NET AppPool”将被自动禁用
- CSS-Sprite-Generator丨CSS雪碧在线生成器
- Java 简单登录MVC
- 使用repeater控件显示列表替代treeview
- Network 20Q--Q2 How does Google sell ad spaces?
- Qgis插件开发之Qgis源码学习
- c++在string类源
- SSLv3协议、TLSv1.2协议配置不对导致javax.ws.rs.ProcessingException: java.net.SocketException: Connection reset
- css实现遮罩层,父div透明,子div不透明
- Django学习(3)模板定制
- Docker可视化管理工具Shipyard安装与配置
- jdbc 报错解决办法
- 通过ApplicationContext.getBean(beanName)动态加载数据。
- 深入理解JVM(5)——HotSpot垃圾收集器详解
- windows下端口映射(端口转发)
- update date and keep time
热门文章
- OS_进程调度:C++实现
- Linux Systemd 详细介绍: Unit、Unit File、Systemctl、Target
- android 6.0 权限设置详解
- jni不通过线程c回调java的函数 --总结
- 在采用K8S之前您必须了解的5件事情
- 基于层级表达的高效网络搜索方法 | ICLR 2018
- 【服务器】CentOs7系统使用宝塔面板搭建网站,有FTP配置(保姆式教程)
- Apache POI 操作Excel(1)--POI简介
- 恕我直言你可能真的不会java第10篇-集合元素归约
- Spring Security(五) —— 动手实现一个 IP_Login