最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

web.py 十分鐘創(chuàng)建簡(jiǎn)易博客實(shí)現(xiàn)代碼

 更新時(shí)間:2016年04月22日 22:04:53   作者:TCM-caleng  
web.py是一款輕量級(jí)的Python web開(kāi)發(fā)框架,簡(jiǎn)單、高效、學(xué)習(xí)成本低,特別適合作為python web開(kāi)發(fā)的入門(mén)框架

一、web.py簡(jiǎn)介
web.py是一款輕量級(jí)的Python web開(kāi)發(fā)框架,簡(jiǎn)單、高效、學(xué)習(xí)成本低,特別適合作為python web開(kāi)發(fā)的入門(mén)框架。官方站點(diǎn):http://webpy.org/

二、web.py安裝
1、下載:http://webpy.org/static/web.py-0.33.tar.gz
2、解壓并進(jìn)入web.py-0.33目錄,安裝:python setup.py install

三、創(chuàng)建簡(jiǎn)易博客
1、目錄說(shuō)明:主目錄blog/,模板目錄blog/templates
2、在數(shù)據(jù)庫(kù)“test”中創(chuàng)建表“entries”

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

3、在主目錄創(chuàng)建blog.py,blog/blog.py

#載入框架
import web
#載入數(shù)據(jù)庫(kù)操作model(稍后創(chuàng)建)
import model
#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,
}
#指定模板目錄,并設(shè)定公共模板
render = web.template.render('templates', base='base', globals=t_globals)
#創(chuàng)建登錄表單
login = web.form.Form(
      web.form.Textbox('username'),
      web.form.Password('password'),
      web.form.Button('login')
      )
#首頁(yè)類(lèi)
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('/')
#查看文章類(lèi)
class View:
 def GET(self, id):
  post = model.get_post(int(id))
  return render.view(post)
#新建文章類(lèi)
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('/')
#刪除文章類(lèi)
class Delete:
 def POST(self, id):
  model.del_post(int(id))
  raise web.seeother('/')
#編輯文章類(lèi)
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('/')
#定義404錯(cuò)誤顯示內(nèi)容
def notfound():
 return web.notfound("Sorry, the page you were looking for was not found.")
 
app.notfound = notfound
#運(yùn)行
if __name__ == '__main__':
 app.run()

4、在主目錄創(chuàng)建model.py,blog/model.py

import web
import datetime
#數(shù)據(jù)庫(kù)連接
db = web.database(dbn = 'MySQL', db = 'test', user = 'root', pw = '123456')
#獲取所有文章
def get_posts():
 return db.select('entries', order = 'id DESC')
 
#獲取文章內(nèi)容
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、在模板目錄依次創(chuàng)建:base.html、edit.html、index.html、new.html、view.html

<!-- base.html -->
$def with (page)
<html>
 <head>
  <title>My Blog</title>
  <mce:style><!--
   #menu {
    width: 200px;
    float: right;
   }
  
--></mce:style><style mce_bogus="1">   #menu {
    width: 200px;
    float: right;
   }
  </style>
 </head>
 
 <body>
  <ul id="menu">
   <li><a href="/" mce_href="">Home</a></li>
   $if cookie().get('username'):
    <li><a href="/new" mce_href="new">New Post</a></li>
  </ul>
  
  $:page
 </body>
</html>

<!-- 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>

<!-- index.html -->
$def with (posts, login_form)
<h1>Blog posts</h1>
$if not cookie().get('username'):
 <form action="" method="post">
 $:login_form.render()
 </form>
$else:
 Welcome $cookie().get('username')!<a href="/logout" mce_href="logout">Logout</a>
<ul>
 $for post in posts:
  <li>
   <a href="/view/$post.id" mce_href="view/$post.id">$post.title</a>
   on $post.posted_on
   $if cookie().get('username'):
    <a href="/edit/$post.id" mce_href="edit/$post.id">Edit</a>
    <a href="/delete/$post.id" mce_href="delete/$post.id">Del</a>
  </li>
</ul>

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

<!-- view.html -->
$def with (post)
<h1>$post.title</h1>
$post.posted_on<br />
$post.content

6、進(jìn)入主目錄在命令行下運(yùn)行:python blog.py,將啟動(dòng)web服務(wù),在瀏覽器輸入:http://localhost:8080/,簡(jiǎn)易博客即已完成。

相關(guān)文章

  • pygame實(shí)現(xiàn)打字游戲

    pygame實(shí)現(xiàn)打字游戲

    這篇文章主要為大家詳細(xì)介紹了pygame實(shí)現(xiàn)打字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • python多進(jìn)程并行代碼實(shí)例

    python多進(jìn)程并行代碼實(shí)例

    這篇文章主要介紹了python多進(jìn)程并行代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • python庫(kù)安裝與使用示例詳解

    python庫(kù)安裝與使用示例詳解

    這篇文章主要介紹了Python中的生成器函數(shù)yield、openslide庫(kù)、ASAP庫(kù)、concurrent.futures.ThreadPoolExecutor、xml.etree.ElementTree庫(kù)、skimage庫(kù)和PIL.Image庫(kù)的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2025-02-02
  • 利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法

    利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法

    今天小編就為大家分享一篇利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Python中向一個(gè)集合添加值的操作方法

    Python中向一個(gè)集合添加值的操作方法

    從數(shù)學(xué)上講,集合是一個(gè)在邏輯上有聯(lián)系的不同對(duì)象的集合,在Python中,集合是一個(gè)內(nèi)置的數(shù)據(jù)類(lèi)型,它是無(wú)索引的和不可變的,這篇文章主要介紹了Python中向一個(gè)集合添加值的操作方法,需要的朋友可以參考下
    2023-10-10
  • 基于Python實(shí)現(xiàn)炸彈人小游戲

    基于Python實(shí)現(xiàn)炸彈人小游戲

    這篇文章主要介紹了基于Python中的Pygame模塊實(shí)現(xiàn)的炸彈人小游戲,文中的示例代碼講解詳細(xì),對(duì)學(xué)習(xí)Python有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下
    2021-12-12
  • PyQt5 界面顯示無(wú)響應(yīng)的實(shí)現(xiàn)

    PyQt5 界面顯示無(wú)響應(yīng)的實(shí)現(xiàn)

    這篇文章主要介紹了PyQt5 界面顯示無(wú)響應(yīng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python中文分詞+詞頻統(tǒng)計(jì)的實(shí)現(xiàn)步驟

    python中文分詞+詞頻統(tǒng)計(jì)的實(shí)現(xiàn)步驟

    詞頻統(tǒng)計(jì)就是輸入一段句子或者一篇文章,然后統(tǒng)計(jì)句子中每個(gè)單詞出現(xiàn)的次數(shù),下面這篇文章主要給大家介紹了關(guān)于python中文分詞+詞頻統(tǒng)計(jì)的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • 如何利用Python合并兩張圖片

    如何利用Python合并兩張圖片

    在Python中可以使用PIL庫(kù)(Python Imaging Library)來(lái)合并兩張圖片,這篇文章主要給大家介紹了關(guān)于如何利用Python合并兩張圖片的相關(guān)資料,文中給了詳細(xì)的代碼示例,需要的朋友可以參考下
    2024-03-03
  • 基于Python編寫(xiě)一個(gè)簡(jiǎn)單的搖號(hào)系統(tǒng)

    基于Python編寫(xiě)一個(gè)簡(jiǎn)單的搖號(hào)系統(tǒng)

    在現(xiàn)代社會(huì)中,搖號(hào)系統(tǒng)廣泛應(yīng)用于車(chē)牌搖號(hào)、房屋搖號(hào)等公共資源分配領(lǐng)域,本文將詳細(xì)介紹如何使用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的搖號(hào)系統(tǒng),有需要的可以了解下
    2024-11-11

最新評(píng)論

信丰县| 新绛县| 灵宝市| 新兴县| 肇源县| 车致| 绥阳县| 博野县| 江永县| 三亚市| 佛教| 富裕县| 菏泽市| 泰州市| 揭西县| 栖霞市| 黄石市| 即墨市| 东阿县| 岳西县| 靖宇县| 印江| 方山县| 西盟| 昆明市| 嘉祥县| 宁海县| 满洲里市| 云和县| 靖安县| 和林格尔县| 策勒县| 留坝县| 永顺县| 汽车| 安丘市| 建水县| 宣威市| 同心县| 博兴县| 石首市|