架构:
Templates
get_classes.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <a href="/add_classes.html">添加</a> </div> <div> <table border="1"> <thead> <tr> <th>ID</th> <th>名称</th> <th>任课老师</th> <th>操作</th> </tr> </thead> <tbody> {% for row in cls_list %} <tr> <td> {{ row.id }} </td> <td> {{ row.title }} </td> <td> {# 班级对象里面,通过.m.all就可以获得老师对象,然后将老师对象循环,显示老师名称#} {% for item in row.m.all %} <span>{{ item.name }}</span> {% endfor %} </td> <td> <a href="/delete.html?nid={{ row.id }}">删除</a> <a href="/update.html?nid={{ row.id }}">修改</a> <a href="/set_teachers.html?nid={{ row.id }}">分配老师</a> </td> </tr> {% endfor %} </tbody> </table> </div> </body> </html>
get_students.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <a href="/add_students.html">添加</a> </div> <div> <table border="1"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>班级</th> <th>操作1</th> <th>操作2</th> </tr> </thead> <tbody> {% for row in stu %} <tr> <td> {{ row.id }} </td> <td> {{ row.username }} </td> <td> {{ row.age }} </td> <td> {{ row.gender }} </td> <td> {{ row.cs.title }} </td> <td> <a href="/delete_students.html?sid={{ row.id }}">删除</a> </td> <td> <a href="/update_students.html?sid={{ row.id }}">修改</a> </td> </tr> {% endfor %} </tbody> </table> </div> </body> </html>
get_teachers.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <a href="/add_teachers.html">添加</a> </div> <div> <table border="1"> <thead> <tr> <th>ID</th> <th>名称</th> <th>任课班级</th> <th>操作</th> </tr> </thead> <tbody> {% for tea in tea_list %} <tr> <td> {{ tea.id }} </td> <td> {{ tea.name }} </td> <td> {# 班级对象里面,通过.m.all就可以获得老师对象,然后将老师对象循环,显示老师名称#} {% for item in tea.classes_set.all %} <span>{{ item.title }}</span> {% endfor %} </td> <td> <a href="/delete_teachers.html?tid={{ tea.id }}">删除</a> <a href="/update_teachers.html?tid={{ tea.id }}">修改</a> <a href="/set_classes.html?tid={{ tea.id }}">分配班级</a> </td> </tr> {% endfor %} </tbody> </table> </div> </body> </html>
add_classes.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="add_classes.html" method="post"> {% csrf_token%} <input type="text" name="title"> <input type="submit" value="提交"> </form> </body> </html>
add_students.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>添加用户</h3> <form action="/add_students.html" method="post"> {% csrf_token%} <p>姓名:<input type="text" name="username"></p> <p>年龄:<input type="text" name="age"></p> <p> 男:<input type="radio" name="gender" value="男"> 女:<input type="radio" name="gender" value="女"> </p> {# 在一对多时,要用到下拉框的情况下,要去已有的数据库去取数据,取得时候别别忘了对应ID#} <p> <select name="cs"> {% for c in cs %} <option value="{{ c.id }}">{{ c.title }}</option> {% endfor %} </select> </p> <input type="submit" value="提交"> </form> </body> </html>
add_teachers.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="add_teachers.html" method="post"> {% csrf_token%} <input type="text" name="teacher"> <input type="submit" value="提交"> </form> </body> </html>
set_classes.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/set_classes.html?tid={{ tid }}" method="post"> {% csrf_token %} <select multiple size="10" name="classes_ids"> {% for item in all_classes_list %} {% if item.id in id_list %} <option value="{{ item.id }}" selected="selected">{{ item.title }}</option> {% else %} <option value="{{ item.id }}">{{ item.title }}</option> {% endif %} {% endfor %} </select> <input type="submit" value="提交"> </form> </body> </html>
set_teachers.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/set_teachers.html?nid={{ nid }}" method="post"> {% csrf_token %} <select multiple size="10" name="teacher_ids"> {% for item in all_teacher_list %} {# item.id=老师表里面的老师ID#} {% if item.id in id_list %} <option value="{{ item.id }}" selected="selected">{{ item.name }}</option> {% else %} <option value="{{ item.id }}">{{ item.name }}</option> {% endif %} {% endfor %} </select> <input type="submit" value="提交"> </form> </body> </html>
update_classes.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {#<form action="/update.html" method="post">#} {#在你点提交的时候要连着ID一块提交,有两种方法,一种是文本框隐藏提交,一种直接根据?nid提交#} <form action="/update.html?nid={{cls.id}}" method="post"> {% csrf_token%} {# <input type="text" name="id" value="{{cls.id}}" style="display: none">#} {# 编辑时候要先获取到数据#} <input type="text" name="nt" value="{{cls.title}}"> <input type="submit" value="提交"> </form> </body> </html>
update_students.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {#在你点提交的时候要连着ID一块提交,有两种方法,一种是文本框隐藏提交,一种直接根据?sid提交#} <form action="/update_students.html?sid={{stu.id}}" method="post"> {% csrf_token %} <p><input type="text" name="username" value="{{stu.username}}"></p> <p><input type="text" name="age" value="{{stu.age}}"> </p> <p> {# 因为要进行默认判断,如果为男,默认要点在男上。#} {% if stu.gender == "男" %} {# checked="checked" 默认#} 男:<input type="radio" name="gender" checked="checked" value="男"> 女:<input type="radio" name="gender" value="女"> {% else %} 男:<input type="radio" name="gender" value="男"> 女:<input type="radio" name="gender" checked="checked" value="女"> {% endif %} </p> <p> <select name="cls"> {% for c in cs %} {# 判断,进行默认班级的显示#} {% if c.id == stu.cs_id %} {# selected="selected" 默认选取 #} <option value="{{ c.id }}" selected="selected">{{ c.title}}</option> {% else %} <option value="{{ c.id }}">{{ c.title}}</option> {% endif %} {% endfor %} </select> </p> <input type="submit" value="提交"> </form> </body> </html>
update_teachers.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {#<form action="/update.html" method="post">#} {#在你点提交的时候要连着ID一块提交,有两种方法,一种是文本框隐藏提交,一种直接根据?nid提交#} <form action="/update_teachers.html?tid={{teacher_list.id}}" method="post"> {% csrf_token%} {# <input type="text" name="id" value="{{cls.id}}" style="display: none">#} {# 编辑时候要先获取到数据#} <input type="text" name="up_tea" value="{{teacher_list.name}}"> <input type="submit" value="提交"> </form> </body> </html>
Urls.py
from django.conf.urls import url from django.contrib import admin from django.urls import path from app01.views import classes from app01.views import students from app01.views import teachers urlpatterns = [ path('admin/', admin.site.urls), url(r'^classes.html$',classes.get_classes), url(r'^add_classes.html$',classes.add_classes), url(r'^delete.html$',classes.delete_classes), url(r'^update.html$',classes.update_classes), url(r'^set_teachers.html$',classes.get_teacher), url(r'^students.html$',students.get_student), url(r'^add_students.html$',students.add_student), url(r'^delete_students.html$',students.delete_student), url(r'^update_students.html$',students.update_student), url(r'^teachers.html$',teachers.get_teacher), url(r'^add_teachers.html$', teachers.add_teacher), url(r'^delete_teachers.html$',teachers.delete_teacher), url(r'^update_teachers.html$',teachers.update_teacher), url(r'^set_classes.html$',teachers.get_classes), ]
Models.py
from django.db import models # Create your models here. class Classes(models.Model): """ 班级表 """ title=models.CharField(max_length=32) m = models.ManyToManyField("Teachers") class Teachers(models.Model): """ 老师表 """ name=models.CharField(max_length=32) ######################## 单表 ######################## # 增加 # Teachers.objects.create(name='root') # obj = Teachers(name='root') # obj.save() # 查 # Teachers.objects.all() # Teachers.objects.filter(id=1) # Teachers.objects.filter(id=1,name='root') # result = Teachers.objects.filter(id__gt=1) # [obj(id,name),] # result = Teachers.objects.filter(id__gt=1).first() # 删除 # Teachers.objects.filter(id=1).delete() # 改 # Teachers.objects.all().update(name='alex') # Teachers.objects.filter(id=1).update(name='alex') class Student(models.Model): username=models.CharField(max_length=32) age = models.IntegerField() gender=models.CharField(max_length=10) cs=models.ForeignKey(Classes,on_delete=models.CASCADE) # 如果classes在上面不用带引号 ######################## 一对多 ######################## """ 班级: id name 1 3班 2 6班 学生 id username age gender cs_id 1 东北 18 男 1 2 东北1 118 男 2 2 东北1 118 男 1 """ # 增加 # Student.objects.create(username='东北',age=18,gender='男',cs_id=1) # Student.objects.create(username='东北',age=18,gender='男',cs= Classes.objects.filter(id=1).first() ) # 查看 """ ret = Student.objects.all() # [] # [ obj(..),] # [ obj(1 东北 18 男 1),obj(2 东北1 118 男 2),obj(..),] for item in ret: print(item.id) print(item.username) print(item.age) print(item.gender) print(item.cs_id) print(item.cs.id) print(item.cs.name) """ # 删除 # Student.objects.filter(id=1).delete() # Student.objects.filter(cs_id=1).delete() # cid = input('请输入班级ID') # Student.objects.filter(cs_id=cid).delete() # cname = input('请输入班级名称') # Student.objects.filter(cs_id=cid).delete() # Student.objects.filter(cs__name=cname).delete() ######################## 多对多 ######################## # 多对多 """ 班级: id title 1 3班 2 4班 3 5班 老师: id title 1 Alex 2 老妖 3 瞎驴 4 Eric 老师班级关系表(类): id 班级id 老师id 1 1 2 2 1 3 4 2 2 5 2 3 6 2 4 7 1 5 # 增 obj = Classes.objects.filter(id=1).first() #1 3班 obj.m.add(2) obj.m.add([4,3]) # obj = Classes.objects.filter(id=2).first() #1 3班 # obj.m.add(2) # obj.m.add([4,3]) obj = Classes.objects.filter(id=1).first() #1 3班 # 删除 # obj.m.remove([4,3]) # 清空 obj.m.clear() # 重置 obj.m.set([2,3,5]) # 查第三张表 # 把3班的所有老师列举 obj = Classes.objects.filter(id=1).frist() obj.id obj.titile ret = obj.m.all() # 第三张表 # ret是一个 [ 老师1(id,name),obj(id,name) ] """
Views
classes.py
from django.shortcuts import render, HttpResponse, redirect from app01 import models def get_classes(request): cls_list = models.Classes.objects.all() return render(request, "get_classes.html", {"cls_list": cls_list}) def add_classes(request): if request.method == "GET": return render(request, "add_classes.html") elif request.method == "POST": title = request.POST.get('title') models.Classes.objects.create(title=title) return redirect("classes.html") # URL 路径 def delete_classes(request): nid = request.GET.get("nid") models.Classes.objects.filter(id=nid).delete() return redirect("classes.html") def update_classes(request): if request.method == "GET": # 在你点击修改的时候,因为没有限制,所以默认是GET提交 nid = request.GET.get("nid") # 然后就是获得nid cls_list = models.Classes.objects.filter(id=nid).first() # 这个地方必须用first return render(request, "update_classes.html", {"cls": cls_list}) # 或者原始数据 elif request.method == "POST": # 因为设定所以当你点提交的时候 就会执行以下操作 nid = request.GET.get("nid") # 一样先获得ID,后面要根据ID 修改 new_title = request.POST.get("nt") # 获得新输入的名字,nt是前面文本框的name models.Classes.objects.filter(id=nid).update(title=new_title) # 最后根据ID来修改title return redirect("classes.html") # 跳转页面 def get_teacher(request): if request.method == "GET": nid = request.GET.get("nid") cls_obj = models.Classes.objects.filter(id=nid).first() # 获得班级对象 cls_teacher_list = cls_obj.m.all().values_list('id', 'name') # 通过 班级对象.多对多名称.all 获得 老师对象列表 # 当前班级任课老师的ID列表 id_list = list(zip(*cls_teacher_list))[0] if list(zip(*cls_teacher_list)) else [] # 获得老师对象列表的第一个字段,也就是id all_teacher_list = models.Teachers.objects.all() return render(request, "set_teacher.html", { 'id_list': id_list, # 'cls_teacher_list': cls_teacher_list, # 这个是当前班级老师的对象 'all_teacher_list': all_teacher_list, # 这个是所有老师的对象 'nid': nid } ) elif request.method == "POST": nid = request.GET.get("nid") ids = request.POST.getlist('teacher_ids') obj = models.Classes.objects.filter(id=nid).first() obj.m.set(ids) return redirect("classes.html")
students.py
from django.shortcuts import render, HttpResponse, redirect from app01 import models def get_student(request): stu_list = models.Student.objects.all() return render(request, "get_students.html", {"stu": stu_list}) def add_student(request): if request.method == "GET": # 因为添加学生时,要找到已有的班级,所以要先获得班级,到前台页面下拉框中 cs_list = models.Classes.objects.all() return render(request, 'add_students.html', {"cs": cs_list}) elif request.method == "POST": u = request.POST.get("username") a = request.POST.get("age") g = request.POST.get("gender") cls = request.POST.get("cs") models.Student.objects.create( username=u, age=a, gender=g, cs_id=cls ) return redirect("/students.html") def delete_student(request): student_id = request.GET.get("sid") models.Student.objects.filter(id=student_id).delete() return redirect("/students.html") def update_student(request): if request.method == "GET": student_id = request.GET.get("sid") stu_list = models.Student.objects.filter(id=student_id).first() # 获取的是一条数据 cs = models.Classes.objects.values('id', 'title') # 需要啥就查啥,可以all() return render(request, "update_students.html", {"stu": stu_list, "cs": cs}) if request.method == "POST": student_id = request.GET.get("sid") username = request.POST.get("username") age = request.POST.get("age") gender = request.POST.get("gender") cls_id = request.POST.get("cls") models.Student.objects.filter(id=student_id).update( username=username, age=age, gender=gender, cs_id=cls_id ) return redirect('/students.html')
teachers.py
from django.shortcuts import render, HttpResponse, redirect from app01 import models # 遍历老师 def get_teacher(request): tea_list = models.Teachers.objects.all() return render(request, "get_teachers.html", {"tea_list": tea_list}) # 删除老师 def delete_teacher(request): tid = request.GET.get('tid') models.Teachers.objects.filter(id=tid).delete() return redirect("teachers.html") # 添加老师 def add_teacher(request): if request.method == "GET": return render(request, "add_teachers.html") elif request.method == "POST": new_teacher = request.POST.get("teacher") models.Teachers.objects.create(name=new_teacher) return redirect("teachers.html") # 修改老师信息 def update_teacher(request): if request.method == "GET": tid = request.GET.get("tid") teacher_list = models.Teachers.objects.filter(id=tid).first() return render(request, "update_teachers.html", {"teacher_list": teacher_list}) elif request.method=="POST": tid = request.GET.get("tid") n_teacher=request.POST.get("up_tea") models.Teachers.objects.filter(id=tid).update(name=n_teacher) return redirect("teachers.html") # 疑问代码 def get_classes(request): if request.method == "GET": tid = request.GET.get("tid") tea_obj = models.Teachers.objects.filter(id=tid).first() # 获得老师对象 cls_teacher_obj=tea_obj.classes_set.all().values_list('id', 'title') # 通过老师获得对应的classes的对象 id_list = list(zip(*cls_teacher_obj))[0] if list(zip(*cls_teacher_obj)) else [] all_classes_list = models.Classes.objects.all() # for y2 in all_classes_list: # print(y2.id) return render(request, "set_classes.html", { 'id_list': id_list, # 'cls_obj: cls_teacher_list, # 这个是当前班级老师的对象 'all_classes_list': all_classes_list, # 这个是所有老师的对象 'cls_teacher_obj':cls_teacher_obj, 'tid': tid } ) elif request.method == "POST": tid = request.GET.get("tid") ids = request.POST.getlist('classes_ids') obj=models.Teachers.objects.filter(id=tid).first() obj.classes_set.set(ids) return redirect("teachers.html")