Django之学员管理一
程序员文章站
2022-08-11 14:03:56
Django之学员管理一 建表结构: 操作单表:班级表和老师表 views.py ......
django之学员管理一
建表结构:
#班级表class:
id title
1 五年一班
2 五年二班
3 五年三班
4 五年四班
#学生表student:
id name 班级id(fk外键)
1 george 1
2 jack 2
3 tom 2
4 alex 1
#老师表teacher:
id name
1 crystal
2 lee
3 iron man
#老师班级关系表teacher_and_class:
id 老师id 班级id
1 1 1
2 1 2
3 2 3
4 2 4
5 3 1
6 3 2
操作单表:班级表和老师表
views.py
from django.shortcuts import render,redirect
import pymysql
def classes(request):
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.dictcursor)
cursor.execute("select id,title from class")
class_list = cursor.fetchall()
cursor.close()
conn.close()
return render(request,'classes.html',{'class_list': class_list})
def add_class(request):
if request.method == "get":
return render(request,'add_class.html')
else:
print(request.post)
v = request.post.get('title')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.dictcursor)
cursor.execute("insert into class(title) values(%s)",[v,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
def del_class(request):
nid = request.get.get('nid')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.dictcursor)
cursor.execute("delete from class where id=%s",[nid,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
def edit_class(request):
if request.method == "get":
nid = request.get.get('nid')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.dictcursor)
cursor.execute("select id,title from class where id = %s",[nid,])
result = cursor.fetchone()
cursor.close()
conn.close()
print(result)
return render(request,'edit_class.html',{'result':result})
else:
nid = request.get.get('nid')
title = request.post.get('title')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.dictcursor)
cursor.execute("update class set title=%s where id = %s",[title,nid,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes/')
上一篇: es6 async和await
下一篇: Vue.js 3 Step 创建一个组件
推荐阅读