時(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í)的快速上手框架。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ò)程: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>
$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(0,0,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)的安全性等等。關(guān)鍵詞:展示,實(shí)現(xiàn)
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。