• ORM之老师管理


    url部分

    #老师管理
     url(r'^teacher_list/', views.teacher_list,name="teacher_list"),
     url(r'^add_teacher/', views.add_teacher,name="add_teacher"),
     url(r'^edit_teacher/(d+)', views.edit_teacher,name="edit_teacher"),
     url(r'^delete_teacher/(d+)', views.delete_teacher,name="delete_teacher"),
    

       

    modls部分

    from django.db import models
    
    
    # Create your models here.
    """
    班级表
    """
    class Class(models.Model):
        id = models.AutoField(primary_key=True)
        cname = models.CharField(max_length=32, null=True)
        first_day = models.DateField()
    
        def __str__(self):
            return self.cname
    
    """
    老师表
    """
    class Teacher(models.Model):
        id=models.AutoField(primary_key=True)
        tname=models.CharField(max_length=32)
        cid=models.ManyToManyField("Class",null=False)
    

      

    views部分

    from django.shortcuts import render, redirect, HttpResponse
    from django.urls import reverse
    from . import models
    
    
    # Create your views here.
    
    
    # 展示老师
    def teacher_list(request):
        teacher_list = models.Teacher.objects.all()
        return render(request, "teacher_list.html", {"teacher_list": teacher_list})
    
    
    # 添加老师
    def add_teacher(request):
        if request.method == "POST":
            tname = request.POST.get("tname")
            class_ids = request.POST.getlist("class_id")
            new_teacher = models.Teacher.objects.create(tname=tname)
            # 查询所有被选中的班级
            class_objs = models.Class.objects.filter(id__in=class_ids)
            # 四种添加方式
            new_teacher.cid.set(class_objs)
            # new_teacher.cid.add(*class_objs)
            # new_teacher.cid.add(*class_ids)
            # new_teacher.cidd.set(class_ids)
            new_teacher.save()
            return redirect(reverse("teacher_list"))
    
        class_list = models.Class.objects.all()
        return render(request, "add_teacher.html", {"class_list": class_list})
    
    
    # 编辑老师
    def edit_teacher(request, tid):
        teacher_obj = models.Teacher.objects.get(id=tid)
        class_list = models.Class.objects.all()
        if request.method == "POST":
            tname = request.POST.get("tname")
            class_ids = request.POST.getlist("class_id")
            teacher_obj.tname = tname
            teacher_obj.cid.set(class_ids)
            teacher_obj.save()  # 一定记得更新完要保存
            return redirect(reverse("teacher_list"))
    
    
        return render(request, "edit_teacher.html", {"class_list": class_list, "teacher": teacher_obj})
    
    
    #删除老师
    def delete_teacher(request,tid):
        models.Teacher.objects.filter(id=tid).delete()
        return redirect(reverse("teacher_list"))
    

    前端部分

    #teacher_list部分
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <title>Title</title>
    </head>
    
    <body>
    <a href="{% url "add_teacher" %}">添加老师</a>
    <table border="1">
        <thead>
        <tr>
            <th>#</th>
            <th>id</th>
            <th>姓名</th>
            <th>授课班级</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
      {% for teacher in teacher_list %}
          <tr>
          <td>{{ forloop.counter }}</td>
          <td>{{ teacher.id }}</td>
          <td>{{ teacher.tname }}</td>
          <td>
              {% for class in teacher.cid.all %}
              {% if forloop.last %}
                {{ class.cname }}
                  {% else %}
                  {{ class.cname }}
              {% endif %}
              {% endfor %}
          </td>
          <td>
              <a href="{% url "edit_teacher" teacher.id %}">编辑</a>
              <a href="{% url "delete_teacher" teacher.id %}">删除</a>
          </td>
          </tr>
      {% endfor %}
    
    
        </tbody>
    </table>
    </body>
    </html>
    #add_teacher部分

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> </head> <body> <form action="{% url "add_teacher" %}" method="post"> {% csrf_token %} <p>老师姓名<input type="text" name="tname"></p> <label for="class_id">授课班级</label> <select name="class_id" id="class_id" multiple>授课班级 {% for class in class_list %} <option value="{{ class.id }}">{{class.cname}}</option> {% endfor %} </select> <p><input type="submit" value="提交"></p> </form> </body> </html> #edit_teacher部分

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> </head> <body> <form action="{% url "edit_teacher" teacher.id %}" method="post"> {% csrf_token %} <p>老师姓名<input type="text" name="tname" value="{{ teacher.tname }}"></p> <label for="class_id">授课班级</label> <select name="class_id" id="class_id" multiple> {% for class in class_list %} {% if class in teacher.cid.all %} <option value="{{ class.id }}" selected>{{ class.cname }}</option> {% else %} <option value="{{ class.id }}" >{{ class.cname }}</option> {% endif %} {% endfor %} </select> <p><input type="submit" value="提交"></p> </form> </body> </html>

      

  • 相关阅读:
    获取设备和 App 信息
    使用 UICollectionView 实现网格化视图效果
    UIImage 读取图片内存优化
    使用 stretchableImageWithLeftCapWidth 方法实现可伸缩图片
    使用 StoryBoard 实现左右按钮切换图片的浏览效果
    二维码图片生成(扩展知识:创建带圆角效果的图片)
    Objective-C语法之扩展(Extension)的使用
    Objective-C语法之字符串NSString去掉前后空格或回车符(可以是NSCharacterSet类型的其它字符)
    Objective-C语法之可变参数
    什么时候layoutSubview会被调用
  • 原文地址:https://www.cnblogs.com/1996-11-01-614lb/p/8343662.html
Copyright © 2020-2023  润新知