zoukankan      html  css  js  c++  java
  • Django 练习班级管理系统六 -- 编辑老师列表

    修改 views.py

    
    @auth
    def edit_teacher(request, nid):
        if request.method == "GET":
            obj = models.Teacher.objects.get(id=nid)
            
            # 执行后返回元组,每个元组包含传递给values_list() 调用的字段的值
            # obj_cls_list = obj.cls.all().values_list('id')
            # 把元组中的第一个字段取出,组成列表
            # id_list = list(zip(*obj_cls_list))[0]
    
            # 通过 values_list 函数中直接设置 flat=True 返回列表
            id_list = obj.cls.all().values_list('id', flat=True).order_by('id')
            # print(id_list)
            cls_list = models.Classes.objects.all()
    
            return render(request, 'edit_teacher.html', {'obj': obj, 'id_list': id_list, 'cls_list': cls_list})
    
        elif request.method == "POST":
            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')
    

    添加 edit_teacher.html

    {% extends "layout.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 type="text" name="name" value="{{ obj.name }}" />
            </p>
            <p>
                班级:
                <select name="cls" multiple>
                    <!-- 循环 cls_list -->
                    {% for row in cls_list %}
                        <!--  判断 row.id 是否选中 -->
                        {% if row.id in id_list %}
                            <option value="{{ row.id }}" selected="selected">{{ row.caption }}</option>
                            {% else %}
                            <option value="{{ row.id }}">{{ row.caption }}</option>
                        {% endif %}
                    {% endfor %}
                </select>
            </p>
            <input type="submit" value="提交">
        </form>
    {% endblock %}
    
    
    {% block js %}
        <script>
            $(function () {
                $('#menu_teacher').addClass('active');
            })
        </script>
    {% endblock %}
    

    修改 teacher.html

    {% extends "layout.html" %}
    
    {% block css %}
    
            .tag{
                display: inline-block;
                padding: 5px;
                border: 1px solid red;
                background-color: lightpink;
                cursor: pointer;
            }
    
    {% 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 %}
    

    修改 urls.py

    # 注意这里需要 import re_path 来试用正则表达式
    from django.urls import path, re_path
    
    urlpatterns = [
    
        # 添加下面几个
        path('teacher.html', views.handle_teacher),
        path('add_teacher.html', views.add_teacher),
        re_path('edit_teacher-(d+).html', views.edit_teacher),
    
    ]
    

  • 相关阅读:
    FILTER(过滤器)
    HDFS优缺点
    python运算符
    python变量类型
    python变量存储
    python编码问题
    【一:定义】python 简介
    如何学一门新技术
    Django安装
    redis 安装及启动关闭
  • 原文地址:https://www.cnblogs.com/klvchen/p/11156241.html
Copyright © 2011-2022 走看看