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路由系统的更多相关文章

  1. Flask - 路由系统

    目录 Flask - 路由系统 @app.route()装饰器中的常用参数 methods : 当前 url 地址,允许访问的请求方式 endpoint:反向url地址,默认为视图函数名(url_fo ...

  2. Flask ——路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  3. Flask路由系统与模板系统

    路由系统 @app.route('/user/<username>') @app.route('/post/<int:post_id>') @app.route('/post/ ...

  4. Flask最强攻略 - 跟DragonFire学Flask - 第七篇 Flask 中路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  5. python 全栈开发,Day120(路由系统, 实例化Flask的参数, 蓝图(BluePrint), before_request after_request)

    昨日内容回顾 1.Flask: from flask import Flask app = Flask(__name__) # 从源码中可以看出,Flask集成的run方法是由werkzeug中的ru ...

  6. 第七篇 Flask 中路由系统

    1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", me ...

  7. 7,Flask 中路由系统

    Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...

  8. flask 第七篇 路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  9. Flask 之路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

随机推荐

  1. kolla-ansible部署openstack allinone单节点

    环境准备 2 network interfaces 8GB main memory 40GB disk space 1.修改hostname hostnamectl set-hostname koll ...

  2. ubuntu 16.04中limit 修改

    第一,修改/etc/security/limits.conf: * soft nproc 65535* hard nproc 65535* soft nofile 65535* hard nofile ...

  3. JAVAWEB复习笔记-day02

    1.CSS样式优先级 优先级:由上到下,由外到内.优先级越来越高 2.css选择器 html标签选择器 class选择器(.) id选择器(#) 3.优先级 style属性>id选择器>c ...

  4. MATLAB之指定文件读取与读取地址输出

    一.读取指定文件夹下的指定格式文件 (1) 利用命令 uigetdir('','') 参数解释: uigetdir('所要打开的盘地址','对打开的弹出框进行描述') 例如:uigetdir('C:\ ...

  5. DVWA之CSRF

    CSRF:跨站请求伪造攻击 Security:Low 级别分析 核心代码 输入数据,以便Burp代理获得请求参数        这里可以将第一行拿出来进行构造链接, http://202.100.10 ...

  6. @TableField(select=false)

    使用这个注解排除删除标识字段.

  7. jmeter引用jar包的3种方式

    示例 实现对登录密码进行MD5加密 pom文件依赖 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --& ...

  8. NOIP 2012 借教室

    洛谷 P1083 借教室 https://www.luogu.org/problem/P1083 JDOJ 1783: [NOIP2012]借教室 D2 T2 https://neooj.com/ol ...

  9. [LeetCode] 670. Maximum Swap 最大置换

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  10. [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...