web.py 十分钟创建简易博客

一,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,目录说明:主目录博客/,模板目录博客/模板

2,在数据库“测试”中创建表“条目”

 

[python]  查看普通 副本

  1. CREATE TABLE条目(    
  2.     id INT AUTO_INCREMENT,    
  3.     标题文字,    
  4.     内容文字,    
  5.     posted_on DATETIME,    
  6.     主键(id)    
  7. );   

 

3,在主目录创建blog.py,博客/ blog.py

 

[python]  查看普通 副本

  1. #载入框架  
  2. 导入网站  
  3. #载入数据库操作模型(稍后创建)  
  4. 进口模式  
  5. #URL映射  
  6. urls =(  
  7.         '/','索引',  
  8.         '/ view /(/ d +)','查看',  
  9.         '/ new','新',  
  10.         '/ delete /(/ d +)','删除',  
  11.         '/ edit /(/ d +)','编辑',  
  12.         '/ login','登录',  
  13.         '/ logout','退出',  
  14.         )  
  15. app = web.application(urls,globals())  
  16. #模板公共变量  
  17. t_globals = {  
  18.     'datestr':web.datestr,  
  19.     'cookie':web.cookies,  
  20. }  
  21. #指定模板目录,并设定公共模板  
  22. render = web.template.render('templates',base ='base',globals = t_globals)  
  23. #创建登录表单  
  24. login = web.form.Form(  
  25.                       web.form.Textbox( '用户名'),  
  26.                       web.form.Password( '密码'),  
  27.                       web.form.Button( '登录')  
  28.                       )  
  29. #首页类  
  30. 类索引:  
  31.     def GET(个体经营):  
  32.         login_form = login()  
  33.         posts = model.get_posts()  
  34.         return render.index(posts,login_form)  
  35.     def POST(个体经营):  
  36.         login_form = login()  
  37.         如果login_form.validates():  
  38.             如果login_form.d.username =='admin'/  
  39.                 和login_form.d.password =='admin':  
  40.                 web.setcookie('username',login_form.d.username)  
  41.         提高web.seeother('/')  
  42. #查看文章类  
  43. 课程查看:  
  44.     def GET(self,id):  
  45.         post = model.get_post(int(id))  
  46.         return render.view(post)  
  47. #新建文章类  
  48. 新课程:  
  49.     form = web.form.Form(  
  50.                          web.form.Textbox( '标题',  
  51.                          web.form.notnull,  
  52.                          大小= 30,  
  53.                          description ='帖子标题:'),  
  54.                          web.form.Textarea( '内容',  
  55.                          web.form.notnull,  
  56.                          行= 30,  
  57.                          COLS = 80,  
  58.                          description ='发布内容:'),  
  59.                          web.form.Button('帖子条目'),  
  60.                          )  
  61.     def GET(个体经营):  
  62.         form = self.form()  
  63.         return render.new(form)  
  64.     def POST(个体经营):  
  65.         form = self.form()  
  66.         如果不是form.validates():  
  67.             return render.new(form)  
  68.         model.new_post(form.d.title,form.d.content)  
  69.         提高web.seeother('/')  
  70. #删除文章类  
  71. 类删除:  
  72.     def GET(self,id):  
  73.         model.del_post(INT(ID))  
  74.         提高web.seeother('/')  
  75. #编辑文章类  
  76. 课程编辑:  
  77.     def GET(self,id):  
  78.         post = model.get_post(int(id))  
  79.         form = New.form()  
  80.         form.fill(POST)  
  81.         return render.edit(post,form)  
  82.     def POST(self,id):  
  83.         form = New.form()  
  84.         post = model.get_post(int(id))  
  85.         如果不是form.validates():  
  86.             return render.edit(post,form)  
  87.         model.update_post(int(id),form.d.title,form.d.content)  
  88.         提高web.seeother('/')  
  89. #退出登录  
  90. 退出:  
  91.     def GET(个体经营):  
  92.         web.setcookie('username','',expires = -1)  
  93.         提高web.seeother('/')  
  94. #定义404错误显示内容  
  95. def notfound():  
  96.     return web.notfound(“抱歉,找不到您要查找的页面。”)  
  97.       
  98. app.notfound = notfound  
  99. #运行  
  100. 如果__name__ =='__ main__':  
  101.     app.run()  

 

4,在主目录创建model.py,博客/ model.py

 

[python]  查看普通 副本

  1. 导入网站  
  2. 导入日期时间  
  3. #数据库连接  
  4. db = web.database(dbn ='mysql',db ='test',user ='root',pw ='123456')  
  5. #获取所有文章  
  6. def get_posts():  
  7.     return db.select('entries',order ='id DESC')  
  8.       
  9. #获取文章内容  
  10. def get_post(id):  
  11.     尝试:  
  12.         return db.select('entries',where ='id = $ id',vars = locals())[0]  
  13.     除了IndexError:  
  14.         返回无  
  15. #新建文章  
  16. def new_post(标题,文字):  
  17.     db.insert( '条目',  
  18.         title = title,  
  19.         content = text,  
  20.         posted_on = datetime.datetime.utcnow())  
  21. #删除文章  
  22. def del_post(id):  
  23.     db.delete('entries',where ='id = $ id',vars = locals())  
  24.       
  25. #修改文章  
  26. def update_post(id,title,text):  
  27.     db.update( '条目',  
  28.         where ='id = $ id',  
  29.         vars = locals(),  
  30.         title = title,  
  31.         content = text)  

 

5,在模板目录依次创建:base.html文件,edit.html,index.html的,new.html,view.html

 

[xhtml]  查看普通 副本

  1. <! - base.html - >  
  2. $ def with(page)  
  3. <HTML>  
  4.     <HEAD>  
  5.         <title>我的博客</ title>  
  6.         <!MCE:风格> <! -  
  7.             #menu {  
  8.                 宽度:200px;  
  9.                 漂浮:对;  
  10.             }  
  11.           
  12. - > </ mce:style> <style mce_bogus =“1”> #menu {  
  13.                 宽度:200px;  
  14.                 漂浮:对;  
  15.             }  
  16.         </样式>  
  17.     </ HEAD>  
  18.       
  19.     <BODY>  
  20.         <ul id =“menu”>  
  21.             <li> <a href="/" mce_href="">主页</a> </ li>  
  22.             $ if cookie()。get('username'):  
  23.                 <li> <a href="/new" mce_href="new">新帖</a> </ li>  
  24.         </ UL>  
  25.           
  26.         $:页  
  27.     </ BODY>  
  28. </ HTML>  
  29.   
  30. <! - edit.html - >  
  31. $ def with(post,form)  
  32. <h1>编辑$ form.d.title </ h1>  
  33. <form action =“”method =“post”>  
  34.     $:form.render()  
  35. </ FORM>  
  36. <h2>删除帖子</ h2>  
  37. <form action =“/ delete / $ post.id”method =“post”>  
  38.     <input type =“submit”value =“删除帖子”/>  
  39. </ FORM>  
  40.   
  41. <! - index.html - >  
  42. $ def with(posts,login_form)  
  43. <h1>博客帖子</ h1>  
  44. $ if not cookie()。get('username'):  
  45.     <form action =“”method =“post”>  
  46.     $:login_form.render()  
  47.     </ FORM>  
  48. $ ELSE:  
  49.     欢迎$ cookie()。get('用户名')!<a href="/logout" mce_href="logout">退出</a>  
  50. <UL>  
  51.     $帖子帖子:  
  52.         <LI>  
  53.             <a href="/view/$post.id" mce_href="view/$post.id"> $ post.title </a>  
  54.             在$ post.posted_on上  
  55.             $ if cookie()。get('username'):  
  56.                 <a href="/edit/$post.id" mce_href="edit/$post.id">修改</a>  
  57.                 <a href="/delete/$post.id" mce_href="delete/$post.id"> Del </a>  
  58.         </ LI>  
  59. </ UL>  
  60.   
  61. <! - new.html - >  
  62. $ def with(form)  
  63. <h1>新博文</ h1>  
  64. <form action =“”method =“post”>  
  65. $:form.render()  
  66. </ FORM>  
  67.   
  68. <! - view.html - >  
  69. $ def with(post)  
  70. <H1> $ post.title </ H1>  
  71. $ post.posted_on <br />  
  72. $ post.content  

 

6,进入主目录在命令行下运行:python blog.py,将启动web服务,在浏览器输入:http:// localhost:8080 /,简易博客即已完成。

 

注意:html中药以$开头,不能以注释开头(比如<! - view.html - >  )否则会报错

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值