zoukankan      html  css  js  c++  java
  • 第七篇 Flask 中路由系统以及参数

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

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

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

     @app.route() 装饰器中的参数

    methods 

    methods : 当前 url 地址,允许访问的请求方式,默认是GET

    @app.route('/login', methods=['GET', 'POST'])
    # methods指定这个列表后,只能有这几种请求方式,其他的都或被拒绝
    def login():
        if request.method == "GET":
            return render_template("login.html")
        else:
            session['user'] = request.form.get('username')
            return redirect("/")

    endpoint

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

    from flask import url_for
    # 如果定义一个验证session的装饰器,需要应用在多个视图函数时候,会报错
    @app.route('/', endpoint="this_is_index") # 解决报错的其中之一方法就是 反向地址 endpoint
    # 由于两个视图函数都使用同一个装饰时候,相当于两个视图函数重名了,都成了装饰其中的inner函数,这个时候会报错
    # 错误信息类似这种
    AssertionError: View function mapping is overwriting an existing endpoint function: inner
    @wrapper def index():
      print(url_for("this_is_index"))
    return render_template("index.html")

    defaults

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

    from flask import url_for
    
    @app.route('/', endpoint="this_is_index", defaults={"nid": 100})
    @wrapper
    def index(nid):
        print(url_for("this_is_index"))  # 打印:  /
        print(nid)  # 浏览器访问下"/",  打印结果: 100
        return render_template("index.html")

    strict_slashes

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

    @app.route('/login', methods=['GET', 'POST'], strict_slashes=True)
    # 加上trict_slashes=True之后  在浏览器访问login后边加上/就访问不到了 提示 not fount
    def login():
        if request.method == "GET":
            return render_template("login.html")
        else:
            session['user'] = request.form.get('username')
            return redirect("/")

    redirect_to

    redirect_to : url地址重定向

    @app.route('/detail', endpoint="this_is_detail", redirect_to="/info")  # 此时访问 就会永久重定向到  /info
    @wrapper
    # AssertionError: View function mapping is overwriting an existing endpoint function: inner
    # 由于使用内部函数想当于把视图函数名字都修改为了inner 所以报错
    # 解决方法  使用  反向地址 endpoint
    def detail():
        return render_template("detail.html")
    
    @app.route('/info')
    def info():
        return "hello world"

    subdomain 

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

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

    动态参数路由:

    # 使用方法:/<int:nid>  /<string:str> /<nid>   # 默认字符创

    @app.route('/info/<id>') def info(id): print(id) # 浏览器url中输入/info/12 打印结果: 12 return "hello world"

    源码以后解析以后再补

  • 相关阅读:
    Vue让水平滚动条(scroll bar)固定在浏览器的底部,并且同轴联动
    vue横向滚动条,初始化位置
    VUE父子组件传值,以及子组件调用父组件方法
    获取shell脚本所在路径而非执行路径
    免重装完整迁移ubuntu18.04系统方法
    auth.log大量出现pam_unix(cron:session): session opened for user root by (uid=0)解决办法
    禁用vim的visual模式方便拖选
    ssh端口反向代理与内网穿透
    mysql查询时将时间戳转换为时间格式
    浏览器打印控件分享
  • 原文地址:https://www.cnblogs.com/clbao/p/10133343.html
Copyright © 2011-2022 走看看