python Flask框架学习——URL与视图

URL与函数的映射

格式:<converter:variable>
尖括号是固定写法,converterURL转换器variable为变量值,默认类型是字符串类型。

变量规则

序号 URL转换器 描述
1 string 默认类型字符串
2 int 接受整数
3 float 接受浮点型
4 path 接受用作目录分隔符的斜杠
5 uuid 接受uuid类型的字符串
6 any 可以指定多个值

使用示例

  • int

    @app.route('/article/<int:p_id>')
    def one_process(p_id):
        return "article page %d" % p_id
    

    在这里插入图片描述

  • string

    @app.route('/article/content/<c>')
    def two_process(c):
        return "article details: %s" % c
    

    在这里插入图片描述

  • float

    @app.route('/article/float/<float:f>')
    def three_process(f):
        return "article float: %f" % f
    

    在这里插入图片描述

  • path

    @app.route('/article/path/<path:p>')
    def four_process(p):
        return "article path: %s" % p
    

    在这里插入图片描述

  • any

    @app.route('/article/any/<any(blog, email):a>')
    def five_process(a):
        if a == "blog":
            return "article any: 博客%s" % a
        if a == 'email':
            return "article any: 邮箱%s" % a
    

    在这里插入图片描述

  • uuid
    先生成uuid再测试

    import uuid
    
    print(uuid.uuid4())
    # uuid:bce756d8-535a-425a-9e00-cfb524d53521
    
    @app.route('/article/getuuid/<uuid:u>')
    def six_process(u):
        print(u)
        return "article uuid: %s" % u
    

    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44604586/article/details/108997084