Flask路由系统
Flask路由系统
我们之前了解了路由系统是由带参数的装饰器完成的。
路由本质:装饰器和闭包实现的。
设置路由的两种方式
第一种:
@app.route('/index')
def index():
return "index"
我们之前用的装饰器都是不带参数的,执行的时候直接是将函数名作为参数传给装饰器执行。那么带参数的装饰器时如何执行的呢?
我们看一下源码:先从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
"""
我们可以将这个带参数的装饰器分开执行:
1. 先执行decorator = app.route('/index')
2. @decorator: 将函数名参数传给装饰器@decorator
"""
第二种:
通过源码分析,以上带参数的装饰器最后也可以写成:
def index():
return "index"
app.add_url_rule("/index",None,index)
- 注意事项:
- 不要让endpoint重名
- 如果非要重名,必须要保证函数是同一个函数,两个函数同名也不行。
参数
"""
@app.route和app.add_url_rule参数:
1. rule: URL规则
1.1 静态参数路由:/index、/login等
1.2 动态参数路由:/index/<name>,在路由中使用了<变量名>的路由称为动态路由, 动态路由参数<name>会接收字符串和数字类型,但在制定了int时会优先调用该视图。
可以指定int型,如/index/<int:id>,在视图函数中必须有同名的形参来接收
动态参数默认转换器:
DEFAULT_CONVERTERS = {
'default': UnicodeConverter,
'string': UnicodeConverter,
'any': AnyConverter,
'path': PathConverter,
'int': IntegerConverter,
'float': FloatConverter,
'uuid': UUIDConverter,
}
2. endpoint=None: 名称,用于反向生成URL,即: url_for('名称')
3.1路由映射视图函数,endpoint不指定默认为视图函数名(view_func.__name__)
3.2项目中存储视图函数的view_funcs是以{endpoint:view_func}形式存储,因此视图函数不能同名,
3.3在使用自定义装饰器时注意指定唯一的endpoint,以避免在多个视图函数使用自定义装饰器时报错;
3. view_func: 视图函数名称
4. methods=None: 允许的请求方式,如:["GET","POST"]
4.1当前视图函数支持的请求方式(405当前请求方式不被允许),
4.2参数为可迭代对象,请求方式不区分大小写,不设置默认为GET
5. defaults=None: 默认值,当URL中无参数,函数需要参数时,使用defaults={'k':'v'}为函数提供参数
6. redirect_to=None: 重定向到指定地址
永久重定向(301或者308)
应用场景:用户之前收藏的是视图函数对应的路由地址,后来页面不在使用,换了新的路径,为了避免用户收藏的不能访问,因此设置永久重定向
6.1 @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
6.2 def func(adapter, nid):
return "/home/888"
@app.route('/index/<int:nid>', redirect_to=func)
7. strict_slashes=True/False: 对URL最后的 / 符号是否严格要求
如:
1)@app.route('/index',strict_slashes=False),
访问 http://www.xx.com/index/ 或 http://www.xx.com/index均可
2)@app.route('/index',strict_slashes=True)
仅访问 http://www.xx.com/index
8. 补充小知识:falsk中通过视图函数名反向解析请求路径:
8.1 from flask import url_for
8.2 url_for('函数名')==>当前视图函数对应的路由请求路径(具体见知识点2)
FBV:app.add_url_rule('/',endpoint='',view_func=func)
CBV:app.sdd_url_rule('/',endpoint='',view_func=CLASS.as_view(name=''))
"""
CBV的路由匹配
通过dispatch_request进行分配
import functools
from flask import Flask,views
app = Flask(__name__)
def wrapper(func):
@functools.wraps(func)
def inner(*args,**kwargs):
return func(*args,**kwargs)
return inner
class UserView(views.MethodView):
methods = ['GET'] #方法
decorators = [wrapper,] #装饰器
def get(self,*args,**kwargs):
return 'GET'
def post(self,*args,**kwargs):
return 'POST'
app.add_url_rule('/user',None,UserView.as_view('uuuu'))
if __name__ == '__main__':
app.run()
自定义正则匹配
在flask中没有正则匹配,但是我们现在有需要进行正则匹配,所以我们可以自定义正则匹配
from flask import Flask, url_for
from werkzeug.routing import BaseConverter
app = Flask(__name__)
# 定制类
class RegexConverter(BaseConverter):
"""
自定义URL匹配正则表达式
"""
def __init__(self, map, regex):
super(RegexConverter, self).__init__(map)
self.regex = regex
def to_python(self, value):
"""
路由匹配时,匹配成功后传递给视图函数中参数的值
"""
return int(value)
def to_url(self, value):
"""
使用url_for反向生成URL时,传递的参数经过该方法处理,返回的值用于生成URL中的参数
"""
value = super(RegexConverter, self).to_url(value)
return value
# 添加到转换器
app.url_map.converters['reg'] = RegexConverter
"""
1. 用户发送请求
2. flask内部进行正则匹配
3. 调用to_python(正则匹配的结果)方法
4. to_python方法的返回值会交给视图函数的参数
"""
# 使用自定义正则
@app.route('/index/<reg("\d+"):nid>')
def index(nid):
print(nid,type(nid))
print(url_for('index',nid=987))
return "Index"
if __name__ == '__main__':
app.run()
Flask路由系统的更多相关文章
- Flask - 路由系统
目录 Flask - 路由系统 @app.route()装饰器中的常用参数 methods : 当前 url 地址,允许访问的请求方式 endpoint:反向url地址,默认为视图函数名(url_fo ...
- Flask ——路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...
- Flask路由系统与模板系统
路由系统 @app.route('/user/<username>') @app.route('/post/<int:post_id>') @app.route('/post/ ...
- Flask最强攻略 - 跟DragonFire学Flask - 第七篇 Flask 中路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...
- python 全栈开发,Day120(路由系统, 实例化Flask的参数, 蓝图(BluePrint), before_request after_request)
昨日内容回顾 1.Flask: from flask import Flask app = Flask(__name__) # 从源码中可以看出,Flask集成的run方法是由werkzeug中的ru ...
- 第七篇 Flask 中路由系统
1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", me ...
- 7,Flask 中路由系统
Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...
- flask 第七篇 路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...
- Flask 之路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...
随机推荐
- 今晚,玩一玩linux上的DNS
老哥遇到的问题, 我先按正规方式操作一波. 一,安装dns服务 yum install bind bind-utils -y 二,修改/etc/named.conf文件 options { liste ...
- 201871010134-周英杰《面向对象程序设计(java)》第八周学习总结
201871010134-周英杰<面向对象程序设计(java)>八周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个 ...
- django学习-数据库操作接口API--(CRUD)
初试API(数据库操作接口CRUD) 现在我们进入交互式python命令行,尝试一下django为你创建的各种API,通过以下命令打开python命令行: py -3 manage.py shell进 ...
- VirtualBox + vagrant 使用虚拟机
1.VirtualBox下载地址 https://www.virtualbox.org/wiki/Downloads 2.vagrant下载地址 https://www.vagrantup.com/d ...
- 在 Oracle 中使用正则表达式
Oracle使用正则表达式离不开这4个函数: 1.regexp_like 2.regexp_substr 3.regexp_instr 4.regexp_replace 看函数名称大概就能猜到有什么用 ...
- day9_7.9 函数的定义
一.基础 1.什么是函数? 函数就是工具,方便开发人员开发软件,非常简洁的工具. 函数的关键字是def 在函数的编写阶段,只检验其语法是否正确,不检验代码. 在函数的调用阶段,可以通过函数名+()来调 ...
- 怎么解决禅道启动服务mysqlzt时的端口失败
打开Windows任务管理器 查看服务是否有MySQL正在运行,停止服务 启动mysqlzt服务 重新启动禅道
- 快速调用Android虚拟机
由于使用Android studio开发flutter 每次打开软件都需要很长时间,所以我自己使用vscode进行开发,没有虚拟机也开发不了,于是就有了这篇博客的作用啦 在本地桌面新建一个xxx.ba ...
- 记一次linux下安装redis, 设置redis服务, 及添加环境变量
一. redis的安装 cd /opt # ...
- Eclipse:设置自动补全,提高编程效率
一.设置自动补全 1.进入eclipse的window里的perferences页面 2.找到java->Editor->Content Assist设置界面 3.在Auto activa ...