Flask系列 路由系统

Flask路由系统细分

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
    return 'ok'

if __name__ == '__main__':
    app.run()

从这个简单的代码入口,来剖析一下路由@app.route(‘/‘)

route():就是一个加在index()上的装饰器



?```python
def route(self, rule, **options):  # rule:匹配规则,options参数字典
    def decorator(f):
        endpoint = options.pop("endpoint", None)  # 如果option中有endpoint就取出,否则endpoint=None
        self.add_url_rule(rule, endpoint, f, **options)  # f就是装饰器装饰的视图函数 index
        return f
    return decorator

获取的信息:
(1)route传参时可以指定 endpoint = ‘别名‘,endpoint是给这个路由取的别名,用作反向解析,稍后再会介绍。没有传时为None。
(2)主要是执行了add_url_rule方法将匹配规定与视图函数的对应关系添加到路由中

add_url_rule():将匹配规定与视图函数的对应关系添加到路由

@setupmethod
def add_url_rule(self,rule,endpoint=None,view_func=None,provide_automatic_options=None,**options):
    # 其中rule是必须要传入的,endpoint是别名,view_func是函数名
    if endpoint is None:
        endpoint = _endpoint_from_view_func(view_func)  # 如果没有别名就执行该函数,并且将视图函数当做参数传入了,稍后再看
    options["endpoint"] = endpoint
    methods = options.pop("methods", None)  # 如果options中有methods则取出,否则为methods = None

    # 如果methods为None的话,默认为view_func中的methods属性值,或者为('GET',)请求
    if methods is None:
        methods = getattr(view_func, "methods", None) or ("GET",)
    # 如果methods是字符串类型,string_types=>(str, unicode),则抛出异常
    if isinstance(methods, string_types):
        raise TypeError(
            "Allowed methods have to be iterables of strings, "
            'for example: @app.route(..., methods=["POST"])'
        )
    # 循环遍历methods,并转成大写、去重
    methods = set(item.upper() for item in methods)

    required_methods = set(getattr(view_func, "required_methods", ()))

    if provide_automatic_options is None:
        provide_automatic_options = getattr(
            view_func, "provide_automatic_options", None
        )

    if provide_automatic_options is None:
        if "OPTIONS" not in methods:
            provide_automatic_options = True
            required_methods.add("OPTIONS")
        else:
            provide_automatic_options = False

    methods |= required_methods

    rule = self.url_rule_class(rule, methods=methods, **options)
    rule.provide_automatic_options = provide_automatic_options

    self.url_map.add(rule)  # 添加匹配规则
    if view_func is not None:
        old_func = self.view_functions.get(endpoint)  # 默认self.view_functions={},所以old_func=None
        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 与 view_func 对应。{endpoint:view_func}

一眼看到上面的源码是不是懵,在代码中相关的部分添加了注释。获取的信息:
(1)methods是定义该视图函数的请求方式,如果没有指定就默认为get方式
(2)methods传参是不能传字符串类型,应该设置:methods=(‘post‘,),参数不区分大小写,即‘post‘与‘POST’都可以传入
(3)将endpoint的值与函数名对应,比如endpoint=‘xxx‘:则相当于是给视图函数index取了一个别名。如果没有endpoint,执行_endpoint_from_view_func(view_func),endpoint=函数名

def _endpoint_from_view_func(view_func):
    """Internal helper that returns the default endpoint for a given
    function.  This always is the function name.
    """
    assert view_func is not None, "expected view func if endpoint is not provided."
    return view_func.__name__  # 返回的就是函数名

总结:

(1)路由本质上就是执行了add_url_rule函数,所以也可以通过该函数来添加路由app.add_url_rule(‘/‘,endpoint=‘xxx‘,view_func=index)
(2)endpoint:用来指定别名,没有指定就用函数名字
(3)methods:用来指定视图函数的请求方式,没有指定就默认为get方法
(4)url_for:通过别名来做反向路由解析

from flask import Flask, request,redirect,url_for
app = Flask(__name__)
app.debug =True

# @app.route('/')
def index():
    return 'dasdk'
app.add_url_rule('/',endpoint='xxx',view_func=index)  # 用来绑定路由

@app.route('/login',methods=['post','get'])
def login():
    url = url_for('xxx')  # 反向路由解析,url此时指向index视图函数
    return redirect(url)

if __name__ == '__main__':
    app.run()

典型写法

@app.route('/detail/<int:nid>',methods=['GET'],endpoint='detail')

默认转换器

DEFAULT_CONVERTERS = {
    'default':          UnicodeConverter,
    'string':           UnicodeConverter,
    'any':              AnyConverter,
    'path':             PathConverter,
    'int':              IntegerConverter,
    'float':            FloatConverter,
    'uuid':             UUIDConverter,
}

@app.route和app.add_url_rule参数

rule:url规则
view_func:视图函数名称
defaults = None:默认值,当URL中无参数,函数需要参数时,使用defaults = {'k': 'v'}为函数提供参数
endpoint = None:名称,用于反向生成URL,即: url_for('名称')
methods = None:允许的请求方式,如:["GET", "POST"]
strict_slashes = None:对URL最后的“/”符号是否严格要求
    '''
        @app.route('/index', strict_slashes=False)
        #访问http://www.xx.com/index/ 或http://www.xx.com/index均可
        @app.route('/index', strict_slashes=True)
        #仅访问http://www.xx.com/index
    '''
#重定向到指定地址
redirect_to = None,
    '''
        @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
    '''

路由正则匹配

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):
        # 路由匹配时,匹配成功后传递给视图函数中参数的值
        value += 'sb'
        return value

    def to_url(self, value):
        # 使用url_for反向生成URL时,传递的参数经过该方法处理,返回的值用于生成URL中的参数
        val = super(RegexConverter,self).to_url(value)
        return val

app.url_map.converters['regex'] = RegexConverter
@app.route('/index/<regex("\d+"):nid>')
def index(nid):
    # 参数nid就是to_python返回的值
    print(nid)
    print(url_for('index',nid='888'))
    return 'Index'

if __name__ == '__main__':
    app.run()

原文地址:https://www.cnblogs.com/bladecheng/p/11604394.html

时间: 2024-08-30 11:48:40

Flask系列 路由系统的相关文章

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

Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道多少? 请关注跟DragonFire学Flask 之 路由系统 ,这里有你想要的答案 1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/inf

Flask第六篇 Flask中路由系统

Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道多少? 请关注跟DragonFire学Flask 之 路由系统 ,这里有你想要的答案 1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/inf

源码解析flask的路由系统

当我们新建一个flask项目时,pycharm通常已经为项目定义了一个基本路由 @app.route('/') def hello_world(): return 'Hello World!' 此时在浏览器中输入地址http://127.0.0.1:5000,页面会显示出"Hello World!"的字样 如下图所示 那么此时在flask后台程序中,到底发生了什么事情呢?? 在上面的例子中,可以看到对hello_world视图函数被app.route这个有参装假器装饰 来看下app.r

Flask 中路由系统

1. @app.route() 装饰器中的参数 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", methods=["GET", "POST"]) def student_info(): stu_id = int(request.args["id"]) return f"Hello Old boy {stu_id}" # Python3.6的新特性 f&qu

【Flask】路由系统

methods 当前视图支持的请求方法 from flask import Flask,render_template app = Flask(__name__) @app.route("/login", methods=["GET", "POST"]) def student_info(): return "Hello wWrd " 默认不写methods参数只支持GET请求,methods参数是我们重写里面的方法,并不是追

Flask之基于route装饰器的路由系统(源码阅读解析)

一 路由系统 1. 在flask中配置URL和视图函数的路由时,首先需要在main.py中实例化一个app对象: 1 from flask import Flask, render_template 2 3 app = Flask(__name__) 2. 然后通过app实例的route方法装饰视图函数,实现路由的配置: 1 @app.route('/') 2 def hello_world(): 3 return 'Hellow World!' 3. 所有这里需要关注在Flask类里定义的ro

第六篇 Flask中的路由系统

Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道多少? 一.@app.route() 装饰器中的参数 methods :当前 url 地址,允许访问的请求方式 @app.route("/info", methods=["GET", "POST"]) d

Flask中的路由系统

Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 至于为什么这么使用,马上开始介绍 [email protected]() 装饰器中的参数 (1).methods: 当前url地址,允许访问的请求方式,默认支持"GET", 如果想加入新的请求方式,就必须加上"GET" USER = {"usernam

11.2 Flask 配置文件,路由系统

配置文件系统 自定义配置文件 创建一个 setting.py 用于存放配置文件的相关属性 配置文件中可以进行分级继承来区分不同视图的配置文件设置 默认配置文件放在项目根路径下 # settings.py class Base(object): # 所有的都要有用到的配置更改 TEST = True class Dev(Base): DEV = True class Pro(Base): PRO = True 配置文件的设置方式 常用的是以下三种 # 方式1 直接更改 app.config["要更