诛仙法宝附件炼器:[Python]Basic Blog in 0.3
来源:百度文库 编辑:九乡新闻网 时间:2024/07/07 14:54:07
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 webimport model### Url mappingsurls = ('/', 'Index','/view/(\d+)', 'View','/new', 'New','/delete/(\d+)', 'Delete','/edit/(\d+)', 'Edit',)### Templatest_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)$post.title
$datestr(post.posted_on)
$post.content
/templates/new.html
$def with (form)New Blog Post
/templates/base.html
$def with (page)My Blog $:page
/templates/index.html
$def with (posts)Blog posts
$for post in posts:- $post.titlefrom $datestr(post.posted_on)Edit
/templates/edit.html
$def with (post, form)Edit $form.d.title
Delete post
/model.py
import web, datetimedb = 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 Nonedef 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)
[Python]Basic Blog in 0.3
[Python]Basic wiki in 0.3
Python
python
python共享内存 - DK's Blog - 博客园
美国民主基本文献USIA: Basic Readings in U.S. Democracy
BASIC语言
solem's vision blog: Reading and writing .mat files with Python
[Python]python随笔
[Python]Python笔记-
[Python]python编程 FAQ
MATLAB与Visual Basic
Visual Basic的基本知识
Cardiac Basic Physiology
python 数列
[Python]C#程序员初学Python
[Python]Python连接MySQL (例子)
[Python]python for ARM/LINUX
[Python]MySQLdb for Python使用指南
[Python]用Python操作Mysql
Visual Basic入门教程之三
Visual Basic入门教程之四
Basic WebLogic Server Domain 创建
Python raw_input 读取输入值 [Python俱乐部]