欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Django 练习班级管理系统六 -- 编辑老师列表

程序员文章站 2022-03-18 16:40:50
修改 views.py 添加 edit_teacher.html 修改 teacher.html 修改 urls.py ......

修改 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),

]

Django 练习班级管理系统六 -- 编辑老师列表

Django 练习班级管理系统六 -- 编辑老师列表