Bottle实例Todo-List—动态路由中使用正则表达式

来源:互联网 发布:cs软件界面设计 编辑:程序博客网 时间:2024/05/17 10:27

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@bottle.route('/item:item#[0-9]+#')
def show_item(item):
    conn = sqlite3.connect('todo.db')
    c = conn.cursor()
    c.execute("SELECT task FROM todo WHERE id LIKE ?", (item))
    result = c.fetchall()
    c.close()
    if not result:
        return 'This item number does not exist!'
    else:
        return 'Task: %s' %result[0]

bottle.debug(True)
bottle.run(host='127.0.0.1', port=8080, reloader = True)
 本段代码在路由中使用了正则表达式,如在浏览器中输入:http://127.0.0.1:8080/item1、http://127.0.0.1:8080/item2、........会分别显示详细记录的“内容”如下:


说明一个route中的正则表达式就可以代替我们写多条路由,缺少方便了不少!

 

 

 

0 0