zoukankan      html  css  js  c++  java
  • Flask内置URL变量转换器 --

     

    Flask内置URL变量转换器:

     

    转换器通过特定的规则执行,”<转换器: 变量名>”。<int: year>把year的值转换为证书,因此我们可以在视图函数中直接对year变量进行数学计算:

    @app.route('/goback/<int:year>'

    def go_back(year):
        return '<p>Welcom to %s ! </p>' %(2019-year)

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

     

    any转换器:

    需要在转换器后添加括号来给出可选值,在写这个变量的值时,如果在可选值范围内,就会执行视图函数的逻辑,否则会报404错误

     

    @app.route('/colors/<any(blue,white,red):color>')
    def three_colors(color):
        return '<p>Love is patient and kind. Love is not jealous or boastful or proud or rude. </p>'

    if __name__ == '__main__':

        app.run(debug = True)

    如果想再any转换器中传入一个预先定义好的列表,可以通过格式化字符串的方式(使用%或是format()函数)来否建URL规则字符串,例如:

     

    colors = ['blue','white','red']
    @app.route('/colors/<any(%s):color>' %str((colors))[1:-1])
    def three_clors(color):
        return '<p>Love is patient and kind. Love is not jealous or boastful or proud or rude. </p>'

    if __name__ == '__main__':

        app.run(debug = True)



    
    
    
    
  • 相关阅读:
    1254:走出迷宫
    1332:【例2-1】周末舞会
    P1744 采购特价商品 题解(讲解图论)
    p1305 新二叉树
    P1069 细胞分裂
    LOJ #124. 除数函数求和 1
    P4438 [HNOI/AHOI2018]道路
    P4318 完全平方数
    P1447 [NOI2010]能量采集
    P3200 [HNOI2009]有趣的数列
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/10386373.html
Copyright © 2011-2022 走看看