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)文章
利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法
今天小編就為大家分享一篇利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
PyQt5 界面顯示無響應(yīng)的實(shí)現(xiàn)
這篇文章主要介紹了PyQt5 界面顯示無響應(yīng)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
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編寫一個(gè)簡單的搖號(hào)系統(tǒng)
在現(xiàn)代社會(huì)中,搖號(hào)系統(tǒng)廣泛應(yīng)用于車牌搖號(hào)、房屋搖號(hào)等公共資源分配領(lǐng)域,本文將詳細(xì)介紹如何使用Python實(shí)現(xiàn)一個(gè)簡單的搖號(hào)系統(tǒng),有需要的可以了解下2024-11-11

