时间:2023-05-25 14:21:02 | 来源:网站运营
时间:2023-05-25 14:21:02 来源:网站运营
python框架web.py,快速实现登录和展示网页开发:python中有几个有名的框架 ,有django这样功能强大的框架,也有flask、web.py这样轻量级的快速上手框架。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的工作过程:import weburls = ( '/', 'index', '/add', 'add', '/delete', 'delete', '/select', 'select', '/st', 'st')# 以上要注意格式,最后一个没有空格。
接着,创建主程序文件import webimport datetimefrom urls import urls# 引入模板文件render = web.template.render("templates/")db = web.database(dbn='mysql', host='数据库IP地址', user='账户', pw='密码', db='数据库名')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()
创建HTML文件index.html,为了方便起见就不使用css来控制样式了。$def with (todos)<div bordor="1"><h2 align="center" color="red">结果展示</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">帐号</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">返回首页</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;}
后面的工作就是持续优化了,比如要设置登陆cookie检测用户是否处于登陆状态,加强系统的安全性等等。关键词:展示,实现