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
接著對(duì)?Django實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)增刪改查(一)的繼續(xù)深入,接著對(duì)學(xué)生管理系統(tǒng)繼續(xù)補(bǔ)充。之前的文中展示了去數(shù)據(jù)庫(kù)中“一對(duì)多”的關(guān)系,本文實(shí)現(xiàn)“多對(duì)多”的邏輯關(guān)系
1.基本框架
1.1.路由分發(fā)
在studentManagementSystem.urls中新增對(duì)于學(xué)生信息的路由
url(r'^students/', views.students),1.2.視圖函數(shù)-邏輯處理
在studentManagementSys/app01/views.py中新增
def students(request):return render(request,'students.html')1.3.模板
在template下新建一個(gè)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>班級(jí)姓名</th><th>所屬班級(jí)</th><th>操作</th></tr></thead><tbody></tbody></table> </body> </html>視圖展示如下:
現(xiàn)在有了以上的“骨架”之后,需要做的就是將對(duì)對(duì)應(yīng)的數(shù)據(jù)進(jìn)行填充,就是豐富其框架
2.查詢功能
按照上面展示信息,需要展示學(xué)生的id,所屬班級(jí),那現(xiàn)在有個(gè)學(xué)生表和有個(gè)班級(jí)表
所以這里使用一個(gè)聯(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:封裝請(qǐng)求相關(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>所屬班級(jí)</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.模板
新增一個(gè)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é)生所屬班級(jí)
那這里就需要考慮,姓名可以輸入,但是班級(jí)是固定的,此時(shí)就需要從已有的班級(jí)中進(jìn)行選擇,就像每年開學(xué)報(bào)名一樣,班級(jí)都是固定的,每個(gè)班級(jí)學(xué)生可以不固定,所以對(duì)add_student.html文件進(jìn)行優(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>所屬班級(jí)<select ><option>Django1班</option></select></p><input type="submit" value="提交" /></form> </body> </html>展示如下所示:
那這個(gè)班級(jí)信息自然去數(shù)據(jù)庫(kù)查詢獲得,這里這里的思路就是,在view.py中add_student中,先查詢出來(lái)班級(jí)信息,然后在add_student.html中使用一個(gè)for循環(huán)展示出班級(jí)信息,這個(gè)在前面的已經(jīng)反復(fù)使用了
所以這里輸入姓名和選擇后班級(jí)后,進(jìn)行提交,這里提交會(huì)涉及到是包含班級(jí)信息提交什么東西到后臺(tái)去,是id還是內(nèi)容?因?yàn)樘峤坏氖菍W(xué)生信息,學(xué)生表中存在的是學(xué)生id,姓名和班級(jí)id信息,所以如果是提交班級(jí)內(nèi)容,還需要到后臺(tái)再次去查詢班級(jí)信息獲取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>所屬班級(jí)<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ù)
同時(shí)在views.py文中新增edit_student處理邏輯
def add_student(request):return render(request,"edit_student.html")如果界面點(diǎn)擊使用該邏輯處理之后,界面展示應(yīng)該是和新增類似
界面上有學(xué)生姓名和所屬班級(jí)信息,編輯之后可以提交,那要編輯班級(jí)信息,此時(shí)必然要在edit_student中查詢班級(jí)信息并放置在下來(lái)列表中,供編輯選擇。結(jié)合一和本文發(fā)現(xiàn)不斷的時(shí)候那幾個(gè)關(guān)于sql的功能,此時(shí)就要想到將該公共處理部分提取出來(lái)
4.3.公共函數(shù)
新增一個(gè)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>所屬班級(jí)<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ù),并沒有實(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})但是這里編輯的時(shí)候返現(xiàn),所欲班級(jí)的地方展示并不是默認(rèn)班級(jí)信息。所以這類需要查詢到每個(gè)學(xué)生默認(rèn)的班級(jí),這里會(huì)使用到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>所屬班級(jí)<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)在需要實(shí)現(xiàn)的是提交功能
<h1>編輯學(xué)生</h1><form method="=POST" action="/edit_student/?nid={{ curent_student_info.id }} ">所以這里的姓名和班級(jí)信息是在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>所屬班級(jí)<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实现对数据库数据增删改查(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django实现对数据库数据增删改查(一
- 下一篇: MySQL的一些基础操作