zoukankan      html  css  js  c++  java
  • Flask 中路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用

    @app.route("/",methods=["GET","POST"])

    为什么要这么用?其中的工作原理我们知道多少?

    请关注跟DragonFire学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"{变量名}"

    endpoint : 反向url地址,默认为视图函数名 (url_for)

    from flask import url_for
    
    
    @app.route("/info", methods=["GET", "POST"], endpoint="r_info")
    def student_info():
        print(url_for("r_info"))  # /info
        stu_id = int(request.args["id"])
        return f"Hello Old boy {stu_id}"  # Python3.6的新特性 f"{变量名}"

    defaults : 视图函数的参数默认值{"nid":1}

    from flask import url_for
    
    
    @app.route("/info", methods=["GET", "POST"], endpoint="r_info", defaults={"nid": 100})
    def student_info(nid):
        print(url_for("r_info"))  # /info
        # stu_id = int(request.args["id"])
        print(nid)  # 100
        return f"Hello Old boy {nid}"  # Python3.6的新特性 f"{变量名}"

    strict_slashes : url地址结尾符"/"的控制 False : 无论结尾 "/" 是否存在均可以访问 , True : 结尾必须不能是 "/"

    # 访问地址 : /info 
    @app.route("/info", strict_slashes=True)
    def student_info():
        return "Hello Old boy info"
    
    
    # 访问地址 : /infos  or  /infos/
    @app.route("/infos", strict_slashes=False)
    def student_infos():
        return "Hello Old boy infos"

    redirect_to : url地址重定向

    # 访问地址 : /info 浏览器跳转至 /infos
    @app.route("/info", strict_slashes=True, redirect_to="/infos")
    def student_info():
        return "Hello Old boy info"
    
    @app.route("/infos", strict_slashes=False)
    def student_infos():
        return "Hello Old boy infos"

    subdomain : 子域名前缀 subdomian="DragonFire" 这样写可以得到 DragonFire.oldboyedu.com 前提是app.config["SERVER_NAME"] = "oldboyedu.com"

    app.config["SERVER_NAME"] = "oldboy.com"
    
    @app.route("/info",subdomain="DragonFire")
    def student_info():
        return "Hello Old boy info"
    
    # 访问地址为:  DragonFire.oldboy.com/info

    关于路由目前就说这么多,之后的课程中会有关于Flask路由系统的源码剖析,再详细说明Flask路由系统的工作原理

    2.动态参数路由:

    from flask import url_for
    
    
    # 访问地址 : http://127.0.0.1:5000/info/1
    @app.route("/info/<int:nid>", methods=["GET", "POST"], endpoint="r_info")
    def student_info(nid):
        print(url_for("r_info",nid=2))  # /info/2
        return f"Hello Old boy {nid}"  # Python3.6的新特性 f"{变量名}"

    <int:nid> 就是在url后定义一个参数接收

    但是这种动态参数路由,在url_for的时候,一定要将动态参数名+参数值添加进去,否则会抛出参数错误的异常

    3.路由正则:

    一般不用,如果有特殊需求,不怕麻烦的话,这个东西还是挺好用的,前提是你的正则玩儿的很6

  • 相关阅读:
    页面监控容器实现记录
    负载均衡基础理论
    asp.net部署时加密config文件
    还原bak到localdb的问题:The logical database file cannot be found ldf
    Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序
    Window vista 以上制作自定义证书并为端口配置ssl
    1-6、算法设计常用思想之迭代法
    1-5、算法设计常用思想之穷举法
    1-4、算法设计常用思想之动态规划法
    游戏开发-cocos creator踩坑-bind(this)导致的事件监听off不掉
  • 原文地址:https://www.cnblogs.com/lyfstorm/p/10056534.html
Copyright © 2011-2022 走看看