from django.db import models
class Classes(models.Model):
title=models.CharField(max_length=32)
m=models.ManyToManyField("Teachers")
class Teachers(models.Model):
name=models.CharField(max_length=32)
class Students(models.Model):
username=models.CharField(max_length=32)
age=models.IntegerField()
gender=models.BooleanField()
cs=models.ForeignKey("Classes")
from django.conf.urls import url
from django.contrib import admin
from app01.views import classes,students,teachers
urlpatterns = [
path('admin/', admin.site.urls),
url(r"^classes.html$", classes.get_classes),
url(r"^add_classes.html$", classes.add_classes),
url(r"^del_classes.html$", classes.del_classes),
url(r"^edi_classes.html$", classes.edi_classes),
url(r"^students.html$", classes.get_students),
url(r"^add_students.html$", classes.add_students),
url(r"^del_students.html$", classes.del_students),
url(r"^edi_students.html$", classes.edi_students),
url(r"^ajax_removeStudent.html$",students.ajax_removeStudent),
url(r"set_teacher.html",teachers.set_teacher),
]
from django.shortcuts import render,redirect
from app01.migrations import models
def get_classes(req):
cls_list=models.Classes.objects.all()
return render(req,"get_classes.html",{"cls_list":cls_list})
def add_classes(req):
if req.method=="GET":
return render(req,"add_classes.html")
elif req.method=="POST":
titl=req.POST.get("title")
models.Classes.objects.create(title=titl)
return redirect("/classes.html")
def del_classes(req):
nid=req.GET.get("nid")
models.Classes.objects.filter(id=nid).delete
return redirect("/classes.html")
def edi_classes(req):
if req.method=="GET":
nid=req.GET.get("nid")
obj=models.Classes.objects.filter(id=nid).first()
return render(req,"edi_classes.html",{"obj":obj})
elif req.method=="POST":
nid=req.GET.get("nid")
tit=req.POST.get("tit")
models.Classes.objects.filter(id=nid).update(title=tit)
return redirect("/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>
{% for item in row.m.all %}
<span>{{item.name}}</span>
{% endfor %}
</td>
<td>
<a href="/del_classes.html?nid={{row.id}}">删除</a>
<a href="/edi_classes.html?nid={{row.id}}">修改</a>
<a href="/set_teacher.html?nid={{row.id}}">分配老师</a>
</td>
</tr>
{% endfor %}
</tody>
</table>
</div>
</body>
</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="提交">
</fomr>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="POST" action="/edi_classes.html?nid={{obj.id}}">
{% csrf_token %}
<input type="text" name="tit" value="{{obj.title}}">
<input type="submit" value="提交">
</form>
</body>
</html>
from django.shortcuts import render,redirect
from app01.migrations import models
def get_students(req):
stu_list=models.Students.objects.all()
return render(req,"get_students.html",{"stu_list":stu_list})
def add_students(req):
if req.method=="GET":
cs_list=models.Classes.objects.all()
for row in cs_list:
print(row.id,row.title)
return render(req,"add_students.html",{"cs_list":cs_list})
elif req.method=="POST":
u=req.POST.get("username")
a=req.POST.get("age")
g=req.POST.get("gender")
c=req.POST.get("cs")
model.Students.objects.create(
username=u
age=a
gender=g
cs_id=c
)
return redirect("/students.html")
def del_students(req):
nid=req.GET.get("nid")
models.Students.objects.filter(id=nid).delete()
return redirect("/students.html")
def edi_students(req):
if req.method=="GET":
nid=req.GET.get("nid")
obj=models.Students.objects.filter(id=nid).first()
cls_list=models.Classes.objects.values("id","title")
return render(req,"edi_students.html",{"obj":obj,"cls_list":cls_list})
elif req.method=="POST":
nid=req.POST.get("nid")
u=req.POST.get("username")
a=req.POST.get("age")
g=req.POST.get("gender")
class_id=req.POST.get("class_id")
models.Students.objects.filter(id=nid).update(username=u,age=a,gender=g,cs_id=class_id)
return redirect("/students.html")
def ajax_removeStudent(req):
nid=req.GET.get("nid")
msg="成功"
try:
models.Students.objects.filter(id=nid).delete()
except Exception as e:
msg=str(e)
return HttpResponse(msg)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<a href="/add_studnets.html">添加</a>
</div>
<div>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>班级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in stu_list %}
<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="/del_students.html?nid={{row.id}}">删除</a>
<a href="#" onclick="removeStudent(this)">Ajax删除</a>
<a href="/edi_students.html?nid={{row.id}}">修改</a>
</td>
</tr>
{% endfor %}
</tody>
</table>
</div>
<script src="/static/jquery-3.5.0.js"></script>
<script>
function removeStudent(ths) {
var nid=$(ths).parent().parent().attr("nid")
$.ajax({
url:"ajax_removeStudent.html",
type:"GET",
data:{nid:nid}
success:function(arg) {
if(arg=="成功") {
<!--window.location.reload();-->
$(ths).parent().parent().remove()
} else {
alert(arg);
}
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/add_students.html">
{% csrf_token %}
<h3>添加用户</h3>
<p><input type="text" name="username" placeholder="用户名"></p>
<p><input type="text" name="age" placeholder="年龄"></p>
<p>
男<input type="radio" name="gender" value="1">
女<input type="radio" name="gender" value="0">
</p>
<p>
<select name="cs">
{% for row in cs_list %}
<option value="{{row.id}}">{{row.title}}</option>
{% endfor %}
</select>
</p>
<input type="submit" value="提交">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/edi_students.html" method="post">
{% csrf_token %}
<p style="display:none"><input type="text" name="nid" value="{{obj.id}}"></p>
<p><input type="text" name="username" value="{{obj.username}}"</p>
<p><input type="text" name="age" value="{{obj.age}}"</p>
<p>
{% if obj.gender %}
男:<input type="radio" name="gender" checked="checked" value="1">
女:<input type="radio" name="gender" value="0">
{% else %}
男:<input type="radio" name="gender" value="1">
女:<input type="radio" name="gender" checked="checked" value="0">
{% endif %}
</p>
<p>
<select name="class_id">
{% for row in cls_list %}
{% if row.id==obj.cs_id %}
<option value="{{row.id}}" selected="selected">{{row.title}}</option>
{% else %}
<option value="{{row.id}}">{{row.title}}</option>
{% endif %}
{% endfor %}
</select>
</p>
<input type="submit" value="提交">
</form>
</body>
</html>
from django.shortcuts import render,redirect
from app01.migrations import models
def set_teacher(req):
if req.method=="GET":
nid=req.GET.get("nid")
cls_obj=models.Classes.objects.filter(id=nid).first()
cls_teacher=cls_obj.m.all().values_list("id")
id_list=list(zip(*cls_teacher))[0] if list(zip(*cls_teacher))[0] else []
all_teacher=models.Teachers.objects.all()
return render(req,"set_teacher.html",{"id_list":id_list,"all_teacher_list":all_teacher,"nid":nid})
elif req.method=="POST":
nid=req.GET.get("nid")
ids=req.GET.getlist("teacher_ids")
obj=models.Classes.objects.filter(id=nid).first()
obj.m.set(ids)
return redirect("/classes.html")
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="set_teacher.html?nid={{nid}}" method="post">
{% csrf_token %}
<select multiple="multiple" name="teacher_ids">
{% for item in all_teacher_list %}
{% if item.id in id_list %}
<option value="{{item.id}}" selected="selected">{{item.name}}</option>
{% elif %}
<option value="{{item.id}}">{{item.name}}</option>
{endif}
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</body>
</html>