代码如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
@bottle.route(
'/edit/:no', method=
'GET')
def edit_item(no): if bottle.request.GET. get( 'save', ''). strip(): edit = bottle.request.GET. get( 'task', ''). strip() status = bottle.request.GET. get( 'status', ''). strip() if status == 'open': status = 1 else: status = 0 conn = sqlite3.connect( 'todo.db') c = conn.cursor() c.execute( "UPDATE todo SET task = ?, status = ? WHERE id LIKE ?", (edit, status, no)) conn.commit() return '<p>The item number %s was successfully updated</p>' % no else: conn = sqlite3.connect( 'todo.db') c = conn.cursor() c.execute( "SELECT task FROM todo WHERE id LIKE ?", ( str(no))) cur_data = c.fetchone() return bottle.template( 'edit_task', old=cur_data, no=no) bottle.debug( True) bottle.run(host= '127.0.0.1', port= 8080, reloader = True) |
edit_task.tpl模板代码如下:
template code
1
2 3 4 5 6 7 8 9 10 11 12 |
%
#template for editing a task % #the template expects to receive a value for "no" as well a "old", the text of the selected ToDo item <p>Edit the task with ID = {{no}}</p> <form action= "/edit/{{no}}" method= "get"> < input type= "text" name= "task" value= "{{old[0]}}" size= "100" maxlength= "100"> <select name= "status"> <option> open</option> <option>closed</option> </select> <br/> < input type= "submit" name= "save" value= "save"> </form> |
同样上面代码用到todo.db数据表,在浏览器中输入:http://127.0.0.1:8080/edit/4
得到如下:
HTML表中的open 和closeed选项分别控制数据标准记录项的"status",的值 0或1: 选open ,点击save,得到如下结果:
同样的方法根据ID(目前有:1、2、3、4、5、6)不同操作各记录项。