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 -
  • 相关阅读:
    数组的基本操作
    面向对象
    继承
    JavaBean规范
    JVM内存模型
    数组的排序
    this
    访问控制权限
    数组
    方法的重载
  • 原文地址:https://www.cnblogs.com/itflycat/p/4435694.html
Copyright © 2011-2022 走看看