通过构建web界面,在web界面上对于数据库进行增删改查的操作。使用python中的web模块完成。
这部分内容目前在网上没有搜到太多类似的,所以有很多地方都是一点点自己试出来的。

另外这里的python程序需要用户联系自身的HTML文件,在本次实验中,我是通过把一部分python代码放到了HTML中,减少了代码的工作量,后来又考虑到URL的原因,所以把所有的程序放到了一个文件中。
import web import model # webpy模块具备URL匹配功能 urls = ( '/','index', '/index1',
'index1', '/index2', 'index2', '/index3', 'index3', '/index4', 'index4',
'/index5', 'index5', '/add','add', '/delete','delete', '/change','change',
'/select','select', '/turn_add','turn_add', '/turn_change','turn_change',
'/turn_delete','turn_delete', '/turn_select','turn_select', '/returnback',
'returnback' ) # 确定程序目录,链接数据库 app = web.application(urls, globals()) render =
web.template.render('templates/') db = web.database(dbn='', host='', port=, db=
'', user='', pw='') # 表示层,web的首页。 class index: def GET(self): return render.
index() # 业务逻辑层,针对具体问题的操作 class turn_add: def POST(self): print("touch it\n")
raise web.seeother('/index1') class turn_delete: def POST(self): print("touch
it\n") raise web.seeother('/index2') class turn_change: def POST(self): print(
"touch it\n") raise web.seeother('/index3') class turn_select: def POST(self):
print("touch it\n") raise web.seeother('/index4') class returnback: def POST(
self): raise web.seeother('/') # 还是表示层 class index: def GET(self): return render
.index() class index1: def GET(self): return render.index1() class index2: def
GET(self): return render.index2() class index3: def GET(self): return render.
index3() class index4: def GET(self): return render.index4() class index5: def
GET(self): return render.index5(email) # 数据访问层,直接操作数据库的语句 class add: def POST(
self): i = web.input() print(i.用户ID) if i.用户ID!='用户ID' and i.用户名!='用户名' and i.
个人简介!='个人简介' and i.所在行业!='所在行业': n = db.insert('user', 用户ID=i.用户ID, 用户名=i.用户名,
个人简介=i.个人简介,所在行业=i.所在行业) raise web.seeother('/')
#POST方法接收到一个post并完成处理后,它将给浏览器发送一个303消息和新网址 else: raise web.seeother('/') class
delete: def POST(self): i = web.input() print(i) a=i.用户ID print(a) db.delete(
'user',where='用户ID = $a',vars=locals()) raise web.seeother('/') class change:
def POST(self): i=web.input() print(i) a=i.用户ID b=i.用户名 if b!='用户名': db.update(
'user',where='用户ID=$a',vars=locals(), 用户名=i.用户名) c=i.个人简介 if c!='个人简介': db.
update('user',where='用户ID=$a',vars=locals(), 个人简介=i.个人简介) d=i.所在行业 if d!='所在行业':
db.update('user',where='用户ID=$a',vars=locals(), 所在行业=i.所在行业) #
db.update('user',where='用户ID=$a',vars=locals(),
用户名=i.用户名,个人简介=i.个人简介,所在行业=i.所在行业) raise web.seeother('/') class select: def
POST(self): i=web.input() print(i) a=i.用户ID if i.用户ID=='用户ID': information = db.
select('user') print(information) return render.index5(information) else: #
之所以要添加vars=locals()这句代码的原因,真的真的很重要!!!!!(坑了我好久) # 更新并以字典形式返回当前局部符号表.
自由变量由函数块的locals() 返回, 而不会由 class 块来返回. 需要注意的是, locals() 字典不应该被修改; 在解释器中对修改行为可能对
local 值或者自由变量无效. # 返回 __dict__ 属性, 比如模块, 类, 实例, 或者其他 带有 __dict__ 属性的 object. 比如
模块和实例拥有可更新的 __dict__ 属性; # 然而其他 objects 可能会对 __dict__ 属性的写操作限制(例如 类使用 dictproxy
阻止直接对字典更新).vars() 如果不传参数 # 那么作用与 locals() 一样. 需要注意的是, locals 字典只在读操作时使用, 因为对
locals 的更新会被忽略. information = db.select('user',where='用户ID=$a',vars=locals())
return render.index5(information) if __name__ == "__main__": app.run()

技术
今日推荐
PPT
阅读数 119
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信