转自:http://blog.csdn.net/caleng/article/details/5712850
一、web.py简介
web.py是一款轻量级的python web开发框架,简单、高效、学习成本低,特别适合作为python web开发的入门框架。官方站点:http://webpy.org/
二、web.py安装
1、下载:http://webpy.org/static/web.py-0.33.tar.gz
2、解压并进入web.py-0.33目录,安装:python setup.py install
三、创建简易博客
1、目录说明:主目录blog/,模板目录blog/templates
2、在数据库“test”中创建表“entries”
- CREATE TABLE entries (
- id INT AUTO_INCREMENT,
- title TEXT,
- content TEXT,
- posted_on DATETIME,
- primary key (id)
- );
3、在主目录创建blog.py,blog/blog.py
-
- import web
-
- import model
-
- urls = (
- '/', 'Index',
- '/view/(/d+)', 'View',
- '/new', 'New',
- '/delete/(/d+)', 'Delete',
- '/edit/(/d+)', 'Edit',
- '/login', 'Login',
- '/logout', 'Logout',
- )
- app = web.application(urls, globals())
-
- t_globals = {
- 'datestr': web.datestr,
- 'cookie': web.cookies,
- }
-
- render = web.template.render('templates', base='base', globals=t_globals)
-
- login = web.form.Form(
- web.form.Textbox('username'),
- web.form.Password('password'),
- web.form.Button('login')
- )
-
- class Index:
- def GET(self):
- login_form = login()
- posts = model.get_posts()
- return render.index(posts, login_form)
- def POST(self):
- login_form = login()
- if login_form.validates():
- if login_form.d.username == 'admin' /
- and login_form.d.password == 'admin':
- web.setcookie('username', login_form.d.username)
- raise web.seeother('/')
-
- class View:
- def GET(self, id):
- 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('/')
-
- class Logout:
- def GET(self):
- web.setcookie('username', '', expires=-1)
- raise web.seeother('/')
-
- def notfound():
- return web.notfound("Sorry, the page you were looking for was not found.")
-
- app.notfound = notfound
-
- if __name__ == '__main__':
- app.run()
4、在主目录创建model.py,blog/model.py
- import web
- import datetime
-
- db = web.database(dbn = 'mysql', db = 'test', user = 'root', pw = '123456')
-
- 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)
5、在模板目录依次创建:base.html、edit.html、index.html、new.html、view.html
-
- $def with (page)
- <html>
- <head>
- <title>My Blogtitle>
- <mce:style>
- $def with (post, form)
- <h1>Edit $form.d.titleh1>
- <form action="" method="post">
- $:form.render()
- form>
- <h2>Delete posth2>
- <form action="/delete/$post.id" method="post">
- <input type="submit" value="Delete post" />
- form>
-
-
- $def with (posts, login_form)
- <h1>Blog postsh1>
- $if not cookie().get('username'):
- <form action="" method="post">
- $:login_form.render()
- form>
- $else:
- Welcome $cookie().get('username')!<a href="/logout" mce_href="logout">Logouta>
- <ul>
- $for post in posts:
- <li>
- <a href="/view/$post.id" mce_href="view/$post.id">$post.titlea>
- on $post.posted_on
- $if cookie().get('username'):
- <a href="/edit/$post.id" mce_href="edit/$post.id">Edita>
- <a href="/delete/$post.id" mce_href="delete/$post.id">Dela>
- li>
- ul>
-
-
- $def with (form)
- <h1>New Blog Posth1>
- <form action="" method="post">
- $:form.render()
- form>
-
-
- $def with (post)
- <h1>$post.titleh1>
- $post.posted_on<br />
- $post.content
6、进入主目录在命令行下运行:python blog.py,将启动web服务,在浏览器输入:http://localhost:8080/,简易博客即已完成。