Django实现对数据库数据增删改查(二)
目錄
1.基本框架
1.1.路由分發(fā)
1.2.視圖函數(shù)-邏輯處理
1.3.模板
2.查詢功能
2.1.視圖函數(shù)
2.2.模板函數(shù)
3.添加功能?
3.1.路由分發(fā)
3.2視圖函數(shù)
3.3.模板
4.編輯功能
4.1路由分發(fā)
4.2.視圖函數(shù)
4.3.公共函數(shù)
4.4.模板
4.5.視圖函數(shù)優(yōu)化1
4.6.視圖函數(shù)優(yōu)化2
接著對?Django實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查(一)的繼續(xù)深入,接著對學(xué)生管理系統(tǒng)繼續(xù)補充。之前的文中展示了去數(shù)據(jù)庫中“一對多”的關(guān)系,本文實現(xiàn)“多對多”的邏輯關(guān)系
1.基本框架
1.1.路由分發(fā)
在studentManagementSystem.urls中新增對于學(xué)生信息的路由
url(r'^students/', views.students),1.2.視圖函數(shù)-邏輯處理
在studentManagementSys/app01/views.py中新增
def students(request):return render(request,'students.html')1.3.模板
在template下新建一個students.html模板
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>學(xué)生列表</h1><div><a ">添加</a></div><table><thead><tr><th>ID</th><th>班級姓名</th><th>所屬班級</th><th>操作</th></tr></thead><tbody></tbody></table> </body> </html>視圖展示如下:
現(xiàn)在有了以上的“骨架”之后,需要做的就是將對對應(yīng)的數(shù)據(jù)進行填充,就是豐富其框架
2.查詢功能
按照上面展示信息,需要展示學(xué)生的id,所屬班級,那現(xiàn)在有個學(xué)生表和有個班級表
所以這里使用一個聯(lián)合查詢的功能
select student.id,student.name,class.title from student left JOIN class on student.class_id= class.id"這樣可以豐富view.py中的student函數(shù)了
2.1.視圖函數(shù)
def students(request):"""學(xué)生列表:param request:封裝請求相關(guān)的所有信息:return:"""# 創(chuàng)建連接conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456',db='studentmanagement',charset='utf8')# 創(chuàng)建游標(biāo)cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù)cursor.execute("select student.id,student.name,class.title from student left JOIN class on student.class_id= class.id")student_list =cursor.fetchall()# 關(guān)閉游標(biāo)cursor.close()# 關(guān)閉連接conn.close()return render(request,'students.html',{'student_list':student_list})2.2.模板函數(shù)
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>學(xué)生列表</h1><div><a >添加</a></div><table><thead><tr><th>學(xué)生ID</th><th>學(xué)生姓名</th><th>所屬班級</th><th>操作</th></tr></thead><tbody>{% for row in student_list %}<tr><td>{{ row.id }}</td><td>{{ row.name }}</td><td>{{ row.title }}</td><td><a >編輯</a>|<a>刪除</a></td></tr>{% endfor %}</tbody></table> </body> </html>輸出界面展示如下:
3.添加功能?
3.1.路由分發(fā)
在url中新增,add_student
url(r'^add_student/',views.add_student),3.2視圖函數(shù)
def add_student(request):return render(request,"add_student.html)3.3.模板
新增一個add_student.html文件
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>添加學(xué)生</h1> </body> </html>這里關(guān)于學(xué)生的添加,需要添加哪些東西是需要考慮的:
- 學(xué)生姓名
- 學(xué)生所屬班級
那這里就需要考慮,姓名可以輸入,但是班級是固定的,此時就需要從已有的班級中進行選擇,就像每年開學(xué)報名一樣,班級都是固定的,每個班級學(xué)生可以不固定,所以對add_student.html文件進行優(yōu)化如下:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>添加學(xué)生</h1><form method="POST" action="/add_student/"><p>學(xué)生姓名<input type="text" name="name"/></p><p>所屬班級<select ><option>Django1班</option></select></p><input type="submit" value="提交" /></form> </body> </html>展示如下所示:
那這個班級信息自然去數(shù)據(jù)庫查詢獲得,這里這里的思路就是,在view.py中add_student中,先查詢出來班級信息,然后在add_student.html中使用一個for循環(huán)展示出班級信息,這個在前面的已經(jīng)反復(fù)使用了
所以這里輸入姓名和選擇后班級后,進行提交,這里提交會涉及到是包含班級信息提交什么東西到后臺去,是id還是內(nèi)容?因為提交的是學(xué)生信息,學(xué)生表中存在的是學(xué)生id,姓名和班級id信息,所以如果是提交班級內(nèi)容,還需要到后臺再次去查詢班級信息獲取id
修改add_student.html文件如下:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>添加學(xué)生</h1><form method="POST" action="/add_student/"><p>學(xué)生姓名<input type="text" name="name"/></p><p>所屬班級<select name="class_id">{% for row in class_list %}<option value="{{ row.id }}">{{ row.title }}</option>{% endfor %}</select></p><input type="submit" value="提交" /></form> </body> </html>修改add_student函數(shù)中的邏輯
def add_student(request):if request.method == 'GET':conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456',db='studentmanagement',charset='utf8')# 創(chuàng)建游標(biāo)cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù)cursor.execute("select id,title from class")class_list =cursor.fetchall()print(class_list)# 關(guān)閉游標(biāo)cursor.close()# 關(guān)閉連接conn.close()return render(request,'add_student.html',{'class_list':class_list})else:name = request.POST.get('name')class_id = request.POST.get('class_id')conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456',db='studentmanagement',charset='utf8')# 創(chuàng)建游標(biāo)cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù)cursor.execute("insert into student(name,class_id) values(%s,%s)",[name,class_id,])conn.commit()# 關(guān)閉游標(biāo)cursor.close()# 關(guān)閉連接conn.close()return redirect('/students/')那學(xué)生刪除和add類似,和第一篇文中的邏輯處理類似
4.編輯功能
4.1路由分發(fā)
url(r'^edit_student/',views.edit_student),4.2.視圖函數(shù)
同時在views.py文中新增edit_student處理邏輯
def add_student(request):return render(request,"edit_student.html")如果界面點擊使用該邏輯處理之后,界面展示應(yīng)該是和新增類似
界面上有學(xué)生姓名和所屬班級信息,編輯之后可以提交,那要編輯班級信息,此時必然要在edit_student中查詢班級信息并放置在下來列表中,供編輯選擇。結(jié)合一和本文發(fā)現(xiàn)不斷的時候那幾個關(guān)于sql的功能,此時就要想到將該公共處理部分提取出來
4.3.公共函數(shù)
新增一個utils模塊,如下所示
import pymysqldef get_list(sql,args):conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456',db='studentmanagement', charset='utf8')# 創(chuàng)建游標(biāo)cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù)cursor.execute(sql,args)result = cursor.fetchall()# 關(guān)閉游標(biāo)cursor.close()# 關(guān)閉連接conn.close()return result def get_one(sql,args):conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456',db='studentmanagement', charset='utf8')# 創(chuàng)建游標(biāo)cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù)cursor.execute(sql,args)result = cursor.fetchone()# 關(guān)閉游標(biāo)cursor.close()# 關(guān)閉連接conn.close()return resultdef modify(sql,args):conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456',db='studentmanagement', charset='utf8')# 創(chuàng)建游標(biāo)cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù)cursor.execute(sql,args)conn.commit()# 關(guān)閉游標(biāo)cursor.close()# 關(guān)閉連接conn.close()4.4.模板
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>編輯學(xué)生</h1><form><p>學(xué)生姓名<input type="text" name="name"/></p><p>所屬班級<select name="class_id">{% for row in class_list %}<option value="{{ row.id }}">{{ row.title }}</option>{% endfor %}</select></p><input type="submit" value="提交" /></form> </body> </html>4.5.視圖函數(shù)優(yōu)化1
剛才只是寫了視圖函數(shù),并沒有實現(xiàn)具體的邏輯功能
def edit_student(request):nid = request.GET.get('nid')print(nid)class_list = sqlhelper.get_list('select id,title from class',[])curent_student_info = sqlhelper.get_one('select id,name,class_id from student where id=%s',nid)return render(request,'edit_student.html',{'class_list':class_list,"curent_student_info":curent_student_info})但是這里編輯的時候返現(xiàn),所欲班級的地方展示并不是默認(rèn)班級信息。所以這類需要查詢到每個學(xué)生默認(rèn)的班級,這里會使用到html-option-selected屬性
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>編輯學(xué)生</h1><form><p>學(xué)生姓名<input type="text" name="name" value="{{ curent_student_info.name }}"/></p><p>所屬班級<select name="class_id">{% for row in class_list %}{% if row.id == curent_student_info.class_id %}<option selected value="{{ row.id }}">{{ row.title }}</option>{% else %}<option value="{{ row.id }}">{{ row.title }}</option>{% endif %}{% endfor %}</select></p><input type="submit" value="提交" /></form> </body> </html>4.6.視圖函數(shù)優(yōu)化2
有了上面的展示默認(rèn)選擇,現(xiàn)在需要實現(xiàn)的是提交功能
<h1>編輯學(xué)生</h1><form method="=POST" action="/edit_student/?nid={{ curent_student_info.id }} ">所以這里的姓名和班級信息是在psot數(shù)據(jù)的body中,學(xué)生id是在url中的?后面的數(shù)據(jù),所以歸結(jié)如下:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>編輯學(xué)生</h1><form method="POST" action="/edit_student/?nid={{ curent_student_info.id }}"><p>學(xué)生姓名<input type="text" name="name" value="{{ curent_student_info.name }}"/></p><p>所屬班級<select name="class_id">{% for row in class_list %}{% if row.id == curent_student_info.class_id %}<option selected value="{{ row.id }}">{{ row.title }}</option>{% else %}<option value="{{ row.id }}">{{ row.title }}</option>{% endif %}{% endfor %}</select></p><input type="submit" value="提交" /></form> </body> </html> from utils import sqlhelper def edit_student(request):if request.method == 'GET':nid = request.GET.get('nid')class_list = sqlhelper.get_list('select id,title from class',[])curent_student_info = sqlhelper.get_one('select id,name,class_id from student where id=%s',[nid,])return render(request,'edit_student.html',{'class_list':class_list,"curent_student_info":curent_student_info})else:nid = request.GET.get('nid')name = request.POST.get('name')class_id = request.POST.get('class_id')print(nid,name,class_id)sqlhelper.modify('update student set name=%s,class_id=%s WHERE id=%s',[name,class_id,nid,])return redirect('/students/')?
總結(jié)
以上是生活随笔為你收集整理的Django实现对数据库数据增删改查(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django实现对数据库数据增删改查(一
- 下一篇: MySQL的一些基础操作