zoukankan      html  css  js  c++  java
  • flask 快速入门03 之 `变量规则`

    变量规则

    在URL部分添加变量使用<variable_name>就可以了。

    @app.route('/user/<username>')
    def show_user_profile(username):
        # show the user profile for that user
        return 'User %s' % username
    
    @app.route('/post/<int:post_id>')
    def show_post(post_id):
        # show the post with the given id, the id is an integer
        return 'Post %d' % post_id  
    

    第一个例子,只要在url后输入什么数据都可以。

    $curl http://127.0.0.1/user/abc
    User abc(site)
    

    第二个例子,因为绑定了转换器规则,所以当不符合转换器指定的类型时,将会报错。

    $curl http://127.0.0.1/post/12345
    Post 12345(site)
    
    $curl http://127.0.0.1/post/abc
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <title>404 Not Found</title>
    <h1>Not Found</h1>
    <p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>
    (site)
    

    int:接收参数必须是整数
    float:接收参数必须是浮点型
    path:接受参数必须是string型,可以接受"/"线。

    关于这个path,其实就是支持字符串的参数。
    我测试了一下。

    加path的情况日志访问的记录

    "GET /user/abc HTTP/1.1" 200 -
    "GET /user/abc/bcd/ HTTP/1.1" 200 -
    "GET /user/abc/bcd HTTP/1.1" 200 -
    "GET /user/abc/bcd/123 HTTP/1.1" 200 -
    

    未加path的记录

    "GET /user/abc/1234/ HTTP/1.1" 404 -
    "GET /user/abc HTTP/1.1" 200 -
    "GET /user/abc/1234/ HTTP/1.1" 404 -
    "GET /user/abc/123 HTTP/1.1" 404 -
  • 相关阅读:
    揭示短线操作宝贵心得
    MFC常用类、成员函数、数组类、Cstring类、CTime类、CPoint类
    A股和B股的区别
    大盘指数的定义及其计算方法
    追涨杀跌法
    成交量变化八规律(旧文有韵)
    蓝筹股、红筹股的含义
    对上市公司进行综合分析
    socket异步笔记
    从WEB SERVICE 上返回大数据量的DATASET
  • 原文地址:https://www.cnblogs.com/itflycat/p/4435694.html
Copyright © 2011-2022 走看看