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. mysql系列3 SQL语法基础

    1.创建数据库(语法) 2.创建(数据库)表(语法) 复制新的空表举个例子: mysql> use course;Reading table information for completion ...

  2. Rust中的枚举及模式匹配

    这个enum的用法,比c要强,和GO类似. enum Coin { Penny, Nickel, Dime, Quarter, } fn value_in_cents(coin: Coin) -> ...

  3. 深入理解defer(上)defer基础

    深入理解 defer 分上下两篇文章,本文为上篇,主要介绍如下内容: 为什么需要 defer: defer 语法及语义: defer 使用要点: defer 语句中的函数到底是在 return 语句之 ...

  4. window.open()与window.showModuleDialog()

    一.window.showModalDialog()     模态对话框. (只支持IE浏览器)window.showModelessDialog()   非模态对话框. 基本语法:vReturnVa ...

  5. C++中#define与typedefine的区别

    原文链接:https://www.cnblogs.com/fengfengqingqingyangyang/p/3270432.html (1)typedef是用来定义关键字/标识符的别名,并未分配内 ...

  6. Mysql 时间和日期函数

    参考文章 1 时间函数 当前日期和时间 select now(); 2 得到昨天的日期 CURDATE() 当前日期 select CURDATE()-1; -- curdate 3 添加时间间隔 当 ...

  7. ACE网络编程:IPC SAP、ACE_SOCKET和TCP/IP通信实例

    socket.TLI.STREAM管道和FIFO为访问局部和全局IPC机制提供广泛的接口.但是,有许多问题与这些不统一的接口有关联.比如类型安全的缺乏和多维度的复杂性会导致成问题的和易错的编程.ACE ...

  8. Linux性能优化实战学习笔记:第三十讲

    一.性能指标 二.文件系统I/O性能指标 1.存储空间的使用情况 文件系统向外展示的空间使用,而非磁盘空间的真是用量,因为文件系统的元数据也会占用磁盘空间 2.索引节点的使用情况 如果存储过多的小文件 ...

  9. Vue插槽详解 | 什么是插槽?

    作者 | Jeskson 来源 | 达达前端小酒馆 什么是插槽?插槽的指令为v-slot,它目前取代了slot和slot-scope,插槽内容,vue实例一套内容分发的api,将slot元素作为承载分 ...

  10. [LeetCode] 731. My Calendar II 我的日历之二

    Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...