web.py 十分鐘創(chuàng)建簡易博客實(shí)現(xiàn)代碼
一、web.py簡介
web.py是一款輕量級(jí)的Python web開發(fā)框架,簡單、高效、學(xué)習(xí)成本低,特別適合作為python web開發(fā)的入門框架。官方站點(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)建簡易博客
1、目錄說明:主目錄blog/,模板目錄blog/templates
2、在數(shù)據(jù)庫“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ù)庫操作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') ) #首頁類 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('/') #定義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ù)庫連接 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/,簡易博客即已完成。
相關(guān)文章
python格式化輸出保留2位小數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了python格式化輸出保留2位小數(shù)的實(shí)現(xiàn)方法,需要的朋友可以參考下2019-07-07何用Python實(shí)現(xiàn)一個(gè) “系統(tǒng)聲音” 的實(shí)時(shí)律動(dòng)掛件
這篇文章將給大家介紹了如何用 Python 實(shí)現(xiàn)一個(gè) “系統(tǒng)聲音” 的實(shí)時(shí)律動(dòng)掛件,采集后直接實(shí)時(shí)地在電腦上繪制波形動(dòng)畫,主要是用來作為 FL Studio 播放時(shí)的一個(gè)桌面小掛件,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-01-01Python?Pygame繪制直線實(shí)現(xiàn)光線反射效果
這篇文章主要為大家詳細(xì)介紹了如何利用Python?Pygame繪制直線以實(shí)現(xiàn)光線反射效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11Python實(shí)現(xiàn)自動(dòng)計(jì)算Excel數(shù)據(jù)指定范圍內(nèi)的區(qū)間最大值
這篇文章主要為大家詳細(xì)介紹了如何基于Python自動(dòng)計(jì)算Excel數(shù)據(jù)指定范圍內(nèi)的區(qū)間最大值,文中的示例代碼簡潔易懂,感興趣的小伙伴可以了解下2023-07-07Python OpenCV實(shí)現(xiàn)視頻追蹤
這篇文章主要為大家詳細(xì)介紹了Python OpenCV實(shí)現(xiàn)視頻追蹤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08python+Matplotlib?繪制帶置信區(qū)間的折線圖
這篇文章主要介紹了python繪制帶置信區(qū)間的折線圖,在本文中,我們將使用?numpy?模塊生成隨機(jī)數(shù)據(jù),并使用?matplotlib?庫實(shí)現(xiàn)數(shù)據(jù)可視化,需要的朋友可以參考下2023-05-05