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