点击上方”python宝典”,关注获取python全套视频,
技术文章第一时间送达!
老师管理
teacher.html
{% extends "base.html" %}
{% block css %}
style
.tag{
display: inline-block;
padding: 5px;
border: 1px solid red;
background-color: lightpink;
cursor: pointer;
}
/style
{% endblock %}
{% block content %}
h1老师列表/h1
div
a href="/add_teacher.html"添加/a
/div
table border="1"
thead/thead
tbody
{% for dic in teacher_list.values %}
tr
td{{ dic.nid }}/td
td{{ dic.name }}/td
td
{% for c in dic.cls_list %}
span class="tag" nid="{{ c.id }}"{{ c.caption }}/span
{% endfor %}
/td
td
a href="/edit_teacher-{{ dic.nid }}.html"编辑/a
/td
/tr
{% endfor %}
/tbody
/table
{% endblock %}
{% block js %}
script
$(function () {
$('#menu_teacher').addClass('active');
})
/script
{% endblock %}
add_teacher.html
{% extends "base.html" %}
{% block css %}
{% endblock %}
{% block content %}
h1添加老师/h1
form action="/add_teacher.html" method="POST"
p
老师姓名:input name="name" type="text" /
/p
p
班级:
select name="cls" multiple
{% for row in cls_list %}
option value="{{ row.id }}"{{ row.caption }}/option
{% endfor %}
/select
/p
input type="submit" value="提交" /
/form
{% endblock %}
{% block js %}
script
$(function () {
$('#menu_teacher').addClass('active');
})
/script
{% endblock %}
edit_teacher.html
{% extends "base.html" %}
{% block css %}
{% endblock %}
{% block content %}
h1编辑老师/h1
form action="/edit_teacher-{{ obj.id }}.html" method="POST"
input style="display: none" type="text" id="nid" value="{{ obj.id }}" /
p
老师姓名:input name="name" type="text" value="{{ obj.name }}" /
/p
p
已管理班级
select id="sel" name="cls" multiple
{% for row in obj_cls_list %}
option value="{{ row.0 }}"{{ row.1 }}/option
{% endfor %}
/select
未管理班级:
select id="none" multiple
{% for row in cls_list %}
option value="{{ row.id }}"{{ row.caption }}/option
{% endfor %}
/select
/p
div
a id="removeCls" /a
a id="addCls" /a
/div
input id="submit_form" type="submit" value="提交" /
/form
{% endblock %}
{% block js %}
script
$(function () {
$('#menu_teacher').addClass('active');
bindRemoveCls();
bindAddCls();
bindSubmitForm();
});
function bindSubmitForm(){
$('#submit_form').click(function () {
// 让select中全选中
$('#sel').children().each(function () {
$(this).prop('selected', true);
})
})
}
function bindRemoveCls() {
$('#removeCls').click(function () {
var options = $('#sel')[0].selectedOptions;
while(options.length0){
$(options[0]).appendTo('#none');
}
})
}
function bindAddCls() {
$('#addCls').click(function () {
var options = $('#none')[0].selectedOptions;
while(options.length0){
$(options[0]).appendTo('#sel');
}
})
}
/script
{% endblock %}
urls.py
from django.contrib import admin
from django.urls import path,re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login.html', views.login),
path('index.html', views.index),
path('logout.html', views.logout),
path('classes.html', views.handle_classes),
path('add_classes.html', views.handle_add_classes),
path('edit_classes.html', views.handle_edit_classes),
path('del_classes.html', views.handle_del_classes),
path('student.html', views.handle_student),
path('add_student.html', views.add_student),
path('edit_student.html', views.edit_student),
path('teacher.html', views.handle_teacher),
path('add_teacher.html', views.add_teacher),
re_path('edit_teacher-(d+).html', views.edit_teacher),
]
veiws.py
@auth
def handle_teacher(request):
# FBV,CBV
current_user = request.session.get('username')
# teacher_list = models.Teacher.objects.all()
# for obj in teacher_list:
# print(obj.id,obj.name,obj.cls.all())
# teacher_list = models.Teacher.objects.filter(id__in=models.Teacher.objects.all()[0:5]).values('id','name','cls__id','cls__caption')
teacher_list = models.Teacher.objects.values('id','name','cls__id','cls__caption')
#构造数据结构
result = {}
for t in teacher_list:
# print(t['id'],t['name'],t['cls__id'],t['cls__caption'])
if t['id'] in result:
if t['cls__id']:
result[t['id']]['cls_list'].append({'id':t['cls__id'], 'caption': t['cls__caption'] })
else:
if t['cls__id']:
temp = [{'id': t['cls__id'], 'caption': t['cls__caption']},]
else:
temp = []
result[t['id']] = {
'nid': t['id'],
'name': t['name'],
'cls_list': temp
}
# result = {
# 1: {
# 'nid': 1,
# 'name': '仓夜空',
# 'cls_list':[
# {'id': 1, 'caption': "python最牛逼"},
# {'id': 2, 'caption': "java最牛逼"}
# ]
# },
# 2: {
# 'nid': 2,
# 'name': '经夜空1',
# 'cls_list': [
# {'id': 1, 'caption': "全栈二班最牛逼"},
# {'id': 5, 'caption': "阿萨德发生地方"}
# ]
# }
# }
return render(request, 'teacher.html', {'username': current_user, "teacher_list": result})
def add_teacher(request):
if request.method == 'GET':
cls_list = models.Classes.objects.all()
return render(request, 'add_teacher.html', {'cls_list': cls_list})
elif request.method == "POST":
name = request.POST.get('name')
cls = request.POST.getlist('cls')
# print(name,cls)
# 创建老师
obj = models.Teacher.objects.create(name=name)
# 创建老师和班级的对应关系
obj.cls.add(*cls)
return redirect('/teacher.html')
def edit_teacher(request,nid):
# 获取当前老师信息
# 获取当前老师对应的所有班级
# - 获取所有的班级
# - 获取当前老师未对应的所有班级
if request.method == "GET":
# 当前老师的信息
obj = models.Teacher.objects.get(id=nid)
# 获取当前老师已经管理的所有班级
# [ (1,"root1"),(2,"root2"),(3,"root3"),]
obj_cls_list = obj.cls.all().values_list('id', 'caption')
# 已经管理的班级的ID列表
id_list = list(zip(*obj_cls_list))[0] if obj_cls_list else []
print(id_list)
# # [1,2,3]
# 获取不包括已经管理的班级,
# cls_list = models.Classes.objects.filter(id__in=id_list)
cls_list = models.Classes.objects.exclude(id__in=id_list)
# # 1
return render(request, 'edit_teacher.html', {'obj': obj,"obj_cls_list": obj_cls_list,"cls_list":cls_list})
elif request.method == "POST":
# nid = request.POST.get('nid')
name = request.POST.get('name')
cls_li = request.POST.getlist('cls')
obj = models.Teacher.objects.get(id=nid)
obj.name = name
obj.save()
obj.cls.set(cls_li)
return redirect('/teacher.html')
至此,这个项目就算完成了。这里只是简单的实现,分页和删除参照第二篇文章。
源码地址:https://pan.baidu.com/s/1cj8WohDRt_3Tk4wdDILotg 密码:ufan
遇到问题欢迎加小编微信一起讨论
识别图中二维码,欢迎关注python宝典