web.py 十分钟创建简易博客实现代码
一、web.py简介
web.py是一款轻量级的Pythonweb开发框架,简单、高效、学习成本低,特别适合作为pythonweb开发的入门框架。官方站点:http://webpy.org/
二、web.py安装
1、下载:http://webpy.org/static/web.py-0.33.tar.gz
2、解压并进入web.py-0.33目录,安装:pythonsetup.pyinstall
三、创建简易博客
1、目录说明:主目录blog/,模板目录blog/templates
2、在数据库“test”中创建表“entries”
CREATETABLEentries( idINTAUTO_INCREMENT, titleTEXT, contentTEXT, posted_onDATETIME, primarykey(id) );
3、在主目录创建blog.py,blog/blog.py
#载入框架 importweb #载入数据库操作model(稍后创建) importmodel #URL映射 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') ) #首页类 classIndex: defGET(self): login_form=login() posts=model.get_posts() returnrender.index(posts,login_form) defPOST(self): login_form=login() iflogin_form.validates(): iflogin_form.d.username=='admin'/ andlogin_form.d.password=='admin': web.setcookie('username',login_form.d.username) raiseweb.seeother('/') #查看文章类 classView: defGET(self,id): post=model.get_post(int(id)) returnrender.view(post) #新建文章类 classNew: form=web.form.Form( web.form.Textbox('title', web.form.notnull, size=30, description='Posttitle:'), web.form.Textarea('content', web.form.notnull, rows=30, cols=80, description='Postcontent:'), web.form.Button('Postentry'), ) defGET(self): form=self.form() returnrender.new(form) defPOST(self): form=self.form() ifnotform.validates(): returnrender.new(form) model.new_post(form.d.title,form.d.content) raiseweb.seeother('/') #删除文章类 classDelete: defPOST(self,id): model.del_post(int(id)) raiseweb.seeother('/') #编辑文章类 classEdit: defGET(self,id): post=model.get_post(int(id)) form=New.form() form.fill(post) returnrender.edit(post,form) defPOST(self,id): form=New.form() post=model.get_post(int(id)) ifnotform.validates(): returnrender.edit(post,form) model.update_post(int(id),form.d.title,form.d.content) raiseweb.seeother('/') #退出登录 classLogout: defGET(self): web.setcookie('username','',expires=-1) raiseweb.seeother('/') #定义404错误显示内容 defnotfound(): returnweb.notfound("Sorry,thepageyouwerelookingforwasnotfound.") app.notfound=notfound #运行 if__name__=='__main__': app.run()
4、在主目录创建model.py,blog/model.py
importweb importdatetime #数据库连接 db=web.database(dbn='MySQL',db='test',user='root',pw='123456') #获取所有文章 defget_posts(): returndb.select('entries',order='idDESC') #获取文章内容 defget_post(id): try: returndb.select('entries',where='id=$id',vars=locals())[0] exceptIndexError: returnNone #新建文章 defnew_post(title,text): db.insert('entries', title=title, content=text, posted_on=datetime.datetime.utcnow()) #删除文章 defdel_post(id): db.delete('entries',where='id=$id',vars=locals()) #修改文章 defupdate_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
<!--base.html--> $defwith(page) <html> <head> <title>MyBlog</title> <mce:style><!-- #menu{ width:200px; float:right; } --></mce:style><stylemce_bogus="1">#menu{ width:200px; float:right; } </style> </head> <body> <ulid="menu"> <li><ahref="/"mce_href="">Home</a></li> $ifcookie().get('username'): <li><ahref="/new"mce_href="new">NewPost</a></li> </ul> $:page </body> </html> <!--edit.html--> $defwith(post,form) <h1>Edit$form.d.title</h1> <formaction=""method="post"> $:form.render() </form> <h2>Deletepost</h2> <formaction="/delete/$post.id"method="post"> <inputtype="submit"value="Deletepost"/> </form> <!--index.html--> $defwith(posts,login_form) <h1>Blogposts</h1> $ifnotcookie().get('username'): <formaction=""method="post"> $:login_form.render() </form> $else: Welcome$cookie().get('username')!<ahref="/logout"mce_href="logout">Logout</a> <ul> $forpostinposts: <li> <ahref="/view/$post.id"mce_href="view/$post.id">$post.title</a> on$post.posted_on $ifcookie().get('username'): <ahref="/edit/$post.id"mce_href="edit/$post.id">Edit</a> <ahref="/delete/$post.id"mce_href="delete/$post.id">Del</a> </li> </ul> <!--new.html--> $defwith(form) <h1>NewBlogPost</h1> <formaction=""method="post"> $:form.render() </form> <!--view.html--> $defwith(post) <h1>$post.title</h1> $post.posted_on<br/> $post.content
6、进入主目录在命令行下运行:pythonblog.py,将启动web服务,在浏览器输入:http://localhost:8080/,简易博客即已完成。