Django之学员管理一
程序员文章站
2022-03-31 12:41:15
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/')
推荐阅读
-
spring-boot-2.0.3不一样系列之番外篇 - 自定义session管理,绝对有值得你看的地方
-
Django之admin管理工具
-
python3之Django表单(一)
-
05.Django基础五之django模型层(一)单表操作
-
abp(net core)+easyui+efcore实现仓储管理系统——EasyUI之货物管理一 (十九)
-
Django基础一之web框架的本质
-
python3.6+django2.0开发一套学员管理系统
-
Django admin 组件 原理分析与扩展使用 之 sites.py (一)
-
python基础入门之十一 —— 函数应用:学员管理系统
-
Django实现学员管理系统