Django框架 练习:学员管理系统-刷新页面版

在这里插入图片描述

#models.py中:
from django.db import models

class Classes(models.Model):
    title=models.CharField(max_length=32)
    m=models.ManyToManyField("Teachers")#如果不加"",需要将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")
#urls.py中:
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")
<!--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>
                            {% for item in row.m.all %}<!--在模板语言中执行函不需要(),故非row.m.all()-->
                                <span>{{item.name}}</span>
                            {% endfor %}
                        </td>
	                    <td>
	                        <a href="/del_classes.html?nid={{row.id}}">删除</a>
	                        <!--不叫id以避免和Python自带的id变量冲突-->
	                        <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>
<!--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="提交">
    </fomr>
</body>
</html>
<!--edi_classes.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}}">
    <!--这样id以GET方式提交,title以POST方式提交-->
        {% csrf_token %}
	    <!--<input type="text" name="id" value={{obj.id}} style="display:none">-->
	    <!--这样id和title就都是以POST方式提交,id标签被设为不可见-->
		<input type="text" name="tit" value="{{obj.title}}">
		<input type="submit" value="提交">
    </form>
</body>
</html>
#students.py中:
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)
<!--get_students.html中:-->
<!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>
	                        <!--不叫id以避免与Python固有的id变量冲突-->
	                        <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>
<!--add_students.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>
<!--edi_students.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>
#teachers.py中:
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 []
        #zip()得到列表[id_list,name_list]的迭代器
        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")
<!--set_teacher.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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值