需求:模拟实现学员管理系统。<*_* 从基础做起>
表结构如下: 班级/学生/老师
班级表: id title
1 xx
2 xx
学生表: id name 班级ID(foreign key)
1 xxx 1
2 xxx 2
老师表: id name
1 xxxx
2 xxx
老师班级关系表(多对多):
id tid(教师id) cid (班级id)
1 1 1
1 2 2
......
1.单表操作
- 增
- 删
- 改
- 查
1 import pymysql 2 # 连接mysql 3 conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘root123456‘, db=‘studentManager‘, 4 charset=‘utf8‘) 5 # 此处的cursor 一定要写成字典类型. 否则查询结构 返回元祖类型 6 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 7 cursor.execute(‘select id,title from class‘) 8 # 返回多个值 9 class_list = cursor.fetchall() 10 cursor.close() 11 conn.close() 12 return render(request, ‘classes.html‘, {‘class_list‘: class_list})
连接mysql
踩坑1:携带查询结果无法在页面上通过循环渲染?
# 通过情况下连接惯性写的是
错误写法:cursor = conn.cursor () # 此处这样写cursor查询结束会将结果封装成元祖类型... 之后通过for循环取值就稳凉...
正确写法:cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 此处这样写cursor查询结果会封装成字典类型. 取值so esay...
--------------------------------------------------------------------------------------------
踩坑2:return render(request, ‘classes.html‘, {‘class_list‘: class_list}) 为撒第一个参数request飘红 ?
麻溜溜去检查函数的形式参数中第一个参数是否写了request...
整体单表操作的思路如下:通过form表单进行数据获取和传递到后台。注意事项有:
- 增、删、改操作结束后重定向到展示班级信息页;
- 携带id传递到后台,一波查询获取数据,返回到修改页.
1 # 显示班级列表 2 # 参数列表上有一个request,切记,切记 3 def classes(request): 4 # 连接mysql 5 conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘root123456‘, db=‘studentManager‘, 6 charset=‘utf8‘) 7 # 此处的cursor 一定要写成字典类型. 否则查询结构 返回元祖类型 8 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 9 cursor.execute(‘select id,title from class‘) 10 # 返回多个值 11 class_list = cursor.fetchall() 12 cursor.close() 13 conn.close() 14 return render(request, ‘classes.html‘, {‘class_list‘: class_list})
展示班级信息--->查
1 def add_class(request): 2 if request.method == ‘GET‘: 3 return render(request, ‘add_class.html‘) 4 else: 5 conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘root123456‘, db=‘studentManager‘, 6 charset=‘utf8‘) 7 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 8 v = request.POST.get(‘title‘) 9 cursor.execute(‘insert into class(title) values (%s)‘, [v, ]) 10 conn.commit() 11 cursor.close() 12 conn.close() 13 return redirect(‘/classes/‘)
添加班级信息--->增
1 def edit_class(request): 2 # 点击编辑按钮,根据Id获取到class信息 传入到下一个页面中 3 if request.method == ‘GET‘: 4 id = request.GET.get(‘nid‘) 5 conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘root123456‘, db=‘studentManager‘, 6 charset=‘utf8‘) 7 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 8 cursor.execute(‘select id,title from class where id = %s‘, [id, ]) 9 result = cursor.fetchone() 10 cursor.close() 11 conn.close() 12 return render(request, ‘edit_class.html‘, {‘result‘: result}) 13 else: 14 id = request.POST.get(‘id‘) 15 title = request.POST.get(‘title‘) 16 conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘root123456‘, db=‘studentManager‘, 17 charset=‘utf8‘) 18 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 19 v = request.POST.get(‘title‘) 20 cursor.execute(‘update class set title =%s where id=%s‘, [title, id]) 21 conn.commit() 22 cursor.close() 23 conn.close() 24 return redirect(‘/classes/‘)
编辑班级信息页--->查&改
1 def del_class(request): 2 id = request.GET.get(‘nid‘) 3 conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘root123456‘, db=‘studentManager‘, 4 charset=‘utf8‘) 5 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 6 cursor.execute(‘delete from class where id = %s‘, [id, ]) 7 conn.commit() 8 cursor.close() 9 conn.close() 10 return redirect(‘/classes/‘)
删除班级信息---->删
注:对于单表操作表单信息量不是很大的情况下,使用Ajax操作会更加丝滑...
原文地址:https://www.cnblogs.com/shine-rainbow/p/9847633.html