web.py是一個(gè)小巧靈活的框架,簡(jiǎn)單且功能強(qiáng)大,源代碼很簡(jiǎn)短。

webpy" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷資訊 > 網(wǎng)站運(yùn)營(yíng) > python框架web.py,快速實(shí)現(xiàn)登錄和展示網(wǎng)頁(yè)開(kāi)發(fā)

python框架web.py,快速實(shí)現(xiàn)登錄和展示網(wǎng)頁(yè)開(kāi)發(fā)

時(shí)間:2023-05-25 14:21:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)

時(shí)間:2023-05-25 14:21:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)

python框架web.py,快速實(shí)現(xiàn)登錄和展示網(wǎng)頁(yè)開(kāi)發(fā):python中有幾個(gè)有名的框架 ,有django這樣功能強(qiáng)大的框架,也有flask、web.py這樣輕量級(jí)的快速上手框架。

web.py是一個(gè)小巧靈活的框架,簡(jiǎn)單且功能強(qiáng)大,源代碼很簡(jiǎn)短。

webpy的作者是Aaron H.Swartz,一位偉大的程序員,他在2013年1月11日自殺身亡,結(jié)束了短暫的26年生命。但是,作為一個(gè)開(kāi)源項(xiàng)目,目前還是有很多開(kāi)發(fā)者在持續(xù)更新。

下面,我們簡(jiǎn)單介紹一下如何使用web.py,快速地構(gòu)建一個(gè)登陸和展示數(shù)據(jù)的網(wǎng)頁(yè)。

我們的目標(biāo)是實(shí)現(xiàn)兩個(gè)功能:

第一個(gè),是讀取數(shù)據(jù)庫(kù)的記錄展示到Web頁(yè)面中,并且實(shí)現(xiàn)增刪改查。

第二個(gè)是實(shí)現(xiàn)一個(gè)登錄認(rèn)證功能。

有了這兩個(gè)功能頁(yè)面,就可以開(kāi)發(fā)一些簡(jiǎn)易的web系統(tǒng)了,畢竟所有的web頁(yè)面除了美工之外,本質(zhì)就是對(duì)數(shù)據(jù)庫(kù)的增刪改查邏輯。

好的,下面我們開(kāi)始。

第一步,安裝Web.py

關(guān)于Web.py的了解可以查看官方網(wǎng)站https://webpy.org/.

官網(wǎng)特別的簡(jiǎn)單,主要就是功能介紹。

在pycharm中使用pip命令安裝 pip install web.py ,默認(rèn)安裝的是0.62版本,pip list展示結(jié)果如下

安裝之后,就可以使用官網(wǎng)提供的示例驗(yàn)證是否成功。

import weburls = ( '/(.*)', 'hello')app = web.application(urls, globals())class hello: def GET(self, name): if not name: name = 'World' return 'Hello, ' + name + '!'if __name__ == "__main__": app.run()一定理解web.py的工作過(guò)程:

  1. 第一步,用戶輸入網(wǎng)址,先由urls進(jìn)行路由分配,就是用戶輸入的網(wǎng)址跳轉(zhuǎn)到某個(gè)類方法來(lái)處理。網(wǎng)址/后面的字符串必須對(duì)應(yīng)某個(gè)事先定義的某個(gè)類的方法,也會(huì)有個(gè)默認(rèn)方法,就是上面都不帶的時(shí)候由默認(rèn)方法處理。比如/add, 主程序中必然有個(gè)類與之對(duì)應(yīng),類的名稱就是urls可以定義的。
  2. 第二步,由這個(gè)類的方法負(fù)責(zé)進(jìn)行具體邏輯的處理,處理的結(jié)果使用Render來(lái)retrun到某個(gè)web頁(yè)面,或者seeother跳轉(zhuǎn)到其它url,然后又是重復(fù)上面的過(guò)程。
  3. 第三步,跳轉(zhuǎn)到具體頁(yè)面時(shí),可以帶上參數(shù),參數(shù)可以理解為需要展示的數(shù)據(jù)集或者某個(gè)處理的反饋結(jié)果。
  4. 第四步,html文件接收到參數(shù)(使用$def with(參數(shù)名))引用,由html頁(yè)面和css負(fù)責(zé)排版來(lái)展示。
只有深刻理解了這個(gè)過(guò)程才能用好Web.py。

第二步 使用Web.py讀取數(shù)據(jù)庫(kù)到Web頁(yè)面

實(shí)現(xiàn)四個(gè)功能,增刪改查。

urls功能說(shuō)明,其中注意/add,后面對(duì)應(yīng)是調(diào)用的類和方法,而不是html文件名稱。先創(chuàng)建urls文件,單獨(dú)配置路由。如下圖所示。

用戶輸入網(wǎng)址,跳轉(zhuǎn)到index方法,add等對(duì)應(yīng)add類,等等。

import weburls = ( '/', 'index', '/add', 'add', '/delete', 'delete', '/select', 'select', '/st', 'st')# 以上要注意格式,最后一個(gè)沒(méi)有空格。接著,創(chuàng)建主程序文件

import webimport datetimefrom urls import urls# 引入模板文件render = web.template.render("templates/")db = web.database(dbn='mysql', host='數(shù)據(jù)庫(kù)IP地址', user='賬戶', pw='密碼', db='數(shù)據(jù)庫(kù)名')app = web.application(urls, globals())# print(datetime.datetime.now())class index: def GET(self): todos = db.select('todo', where='is_delete=0') print(datetime.datetime.now()) return render.index(todos)class select: def POST(self): f = web.input() print(f) todos = db.select('todo', where='id=%s' % f.id) return render.index(todos)class add: def POST(self): i = web.input() # print(datetime.datetime.now()) n = db.insert('todo', title=i.title) # print(datetime.datetime.now()) raise web.seeother('/')class delete: def GET(self): t = int(web.ctx['query'][4: : ]) print(t) print(type(t)) n = db.update('todo', where="id={}".format(t), is_delete=1) # n = db.query('update todo set is_delete=1 where id= t') print(n) raise web.seeother('/')if __name__ == "__main__": app.run()創(chuàng)建HTML文件index.html,為了方便起見(jiàn)就不使用css來(lái)控制樣式了。

$def with (todos)<div bordor="1"><h2 align="center" color="red">結(jié)果展示</h2><form method="post" action="select"><p><input type="text" name="id" /> <input type="submit" value="查詢操作" /></p></form> <table border="1" align="center"> <tr> <th>id</th> <th>name</th> <th>操作</th> </tr> $for todo in todos: <tr> <td> id="$todo.id" </td> <td> $todo.title </td> <td> <a href="/delete?id=$todo.id" class="btn btn-danger btn-xs">刪除</a> </td> </tr></table></div><form method="post" action="add"><p><input type="text" name="title" /> <input type="submit" value="Add" /></p></form>
HTML最終展示頁(yè)面



這是一個(gè)標(biāo)準(zhǔn)的增刪改查功能頁(yè)面,做的比較簡(jiǎn)陋。

不過(guò),已經(jīng)初步具備了使用web.py所有功能的過(guò)程,比如,連接和操作數(shù)據(jù)庫(kù),將數(shù)據(jù)查詢出來(lái)使用html展示,在頁(yè)面中進(jìn)行刪除操作。

第三步,使用css來(lái)控制樣式,提升頁(yè)面的風(fēng)格

最終的效果如下:

login.html文件

$def with ()<form id="login" action="" method="POST"><table align="left"><tbody> <tr> <td><label for="username">帳號(hào)</label></td> <td><input type="text" id="username" name="username" /><span class="validate_tip"></span></td> </tr> <tr> <td><label for="password">密碼</label></td> <td><input type="password" id="password" name="password" /><span class="validate_tip"></span></td> </tr> <tr> <td></td> <td><a href="/stock" id="find_password">返回首頁(yè)</a></td> </tr> <tr><td><input type="submit" id="login_btn" value="登錄" /></td></tr></tbody></table></form>CSS腳本如下

body{ background-color:#00000; margin:0; padding:0; font-family:sans-serif; background:url(../img/back.jpg) no-repeat fixed top;}.sign-up-form { width: 300px; box-shadow: 0 0 3px 0 rgba(00,0,0.3) background: #fff; padding: 20px; margin: 8% auto 0; text-align: center; border:0px solid #000; } .sign-up-form h1{ color: #1c8adb; margin-bottom: 30px;}.input-box{ border-radius:20px; padding:10px; margin:10px 0; width:100%; border:1px solid #999; outline:none;}button{ color: #fff; width: 100%; padding: 10px; border-radius: 20px; font-size: 15px; margin:10px 0; outline:none;}.signup-btn{ background-color: #1c8adb;}后面的工作就是持續(xù)優(yōu)化了,比如要設(shè)置登陸cookie檢測(cè)用戶是否處于登陸狀態(tài),加強(qiáng)系統(tǒng)的安全性等等。

本篇只作為簡(jiǎn)單的入門示例,如果點(diǎn)贊活著關(guān)注多的話,后續(xù)再更新完善這篇文章。

關(guān)鍵詞:展示,實(shí)現(xiàn)

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉