分享

[Python]Basic Blog in 0.3

 思念是一种饼 2011-04-12
on this, so anyone could create a post, but it shows the basic functionality.

Files

/blog.py
/schema.sql
/templates:
/templates/view.html
/templates/new.html
/templates/base.html
/templates/index.html
/templates/edit.html
/model.py

/blog.py

""" Basic blog using webpy 0.3 """
import web
import model
### Url mappings
urls = (
'/', 'Index',
'/view/(\d+)', 'View',
'/new', 'New',
'/delete/(\d+)', 'Delete',
'/edit/(\d+)', 'Edit',
)
### Templates
t_globals = {
'datestr': web.datestr
}
render = web.template.render('templates', base='base', globals=t_globals)
class Index:
def GET(self):
""" Show page """
posts = model.get_posts()
return render.index(posts)
class View:
def GET(self, id):
""" View single post """
post = model.get_post(int(id))
return render.view(post)
class New:
form = web.form.Form(
web.form.Textbox('title', web.form.notnull,
size=30,
description="Post title:"),
web.form.Textarea('content', web.form.notnull,
rows=30, cols=80,
description="Post content:"),
web.form.Button('Post entry'),
)
def GET(self):
form = self.form()
return render.new(form)
def POST(self):
form = self.form()
if not form.validates():
return render.new(form)
model.new_post(form.d.title, form.d.content)
raise web.seeother('/')
class Delete:
def POST(self, id):
model.del_post(int(id))
raise web.seeother('/')
class Edit:
def GET(self, id):
post = model.get_post(int(id))
form = New.form()
form.fill(post)
return render.edit(post, form)
def POST(self, id):
form = New.form()
post = model.get_post(int(id))
if not form.validates():
return render.edit(post, form)
model.update_post(int(id), form.d.title, form.d.content)
raise web.seeother('/')
app = web.application(urls, globals())
if __name__ == '__main__':
app.run()

/schema.sql

CREATE TABLE entries (
id INT AUTO_INCREMENT,
title TEXT,
content TEXT,
posted_on DATETIME,
primary key (id)
);

/templates/view.html

$def with (post)
<h1>$post.title</h1>
$datestr(post.posted_on)<br/>
$post.content

/templates/new.html

$def with (form)
<h1>New Blog Post</h1>
<form action="" method="post">
$:form.render()
</form>

/templates/base.html

$def with (page)
<html>
<head>
<title>My Blog</title>
<style>
#menu {
width: 200px;
float: right;
}
</style>
</head>
<body>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/new">New Post</a></li>
</ul>
$:page
</body>
</html>

/templates/index.html

$def with (posts)
<h1>Blog posts</h1>
<ul>
$for post in posts:
<li>
<a href="/view/$post.id">$post.title</a>
from $datestr(post.posted_on)
<a href="/edit/$post.id">Edit</a>
</li>
</ul>

/templates/edit.html

$def with (post, form)
<h1>Edit $form.d.title</h1>
<form action="" method="post">
$:form.render()
</form>
<h2>Delete post</h2>
<form action="/delete/$post.id" method="post">
<input type="submit" value="Delete post"/>
</form>

/model.py

import web, datetime
db = web.database(dbn='mysql', db='blog', user='justin')
def get_posts():
return db.select('entries', order='id DESC')
def get_post(id):
try:
return db.select('entries', where='id=$id', vars=locals())[0]
except IndexError:
return None
def new_post(title, text):
db.insert('entries', title=title, content=text, posted_on=datetime.datetime.utcnow())
def del_post(id):
db.delete('entries', where="id=$id", vars=locals())
def update_post(id, title, text):
db.update('entries', where="id=$id", vars=locals(),
title=title, content=text)

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多