分享

[Python]Basic Todo List in 0.3

 思念是一种饼 2011-04-12

Todo list

Very basic todo list in 0.3 webpy. Probably the most trivial database-backed app possible.

Files

/schema.sql
/templates:
/templates/base.html
/templates/index.html
/model.py
/todo.py

/schema.sql

CREATE TABLE todo (
id INT AUTO_INCREMENT,
title TEXT,
primary key (id)
);

/templates/base.html

$def with (page)
<html>
<head>
<title>Todo list</title>
</head>
<body>
$:page
</body>
</html>

/templates/index.html

$def with (todos, form)
<table>
<tr>
<th>What to do ?</th>
<th></th>
</tr>
$for todo in todos:
<tr>
<td>$todo.title</td>
<td>
<form action="/del/$todo.id" method="post">
<input type="submit" value="Delete"/>
</form>
</td>
</tr>
</table>
<form action="" method="post">
$:form.render()
</form>

/model.py

import web
db = web.database(dbn='mysql', db='todo', user='justin')
def get_todos():
return db.select('todo', order='id')
def new_todo(text):
db.insert('todo', title=text)
def del_todo(id):
db.delete('todo', where="id=$id", vars=locals())

/todo.py

""" Basic todo list using webpy 0.3 """
import web
import model
### Url mappings
urls = (
'/', 'Index',
'/del/(\d+)', 'Delete'
)
### Templates
render = web.template.render('templates', base='base')
class Index:
form = web.form.Form(
web.form.Textbox('title', web.form.notnull,
description="I need to:"),
web.form.Button('Add todo'),
)
def GET(self):
""" Show page """
todos = model.get_todos()
form = self.form()
return render.index(todos, form)
def POST(self):
""" Add new entry """
form = self.form()
if not form.validates():
todos = model.get_todos()
return render.index(todos, form)
model.new_todo(form.d.title)
raise web.seeother('/')
class Delete:
def POST(self, id):
""" Delete based on ID """
id = int(id)
model.del_todo(id)
raise web.seeother('/')
app = web.application(urls, globals())
if __name__ == '__main__':
app.run()

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多