zoukankan      html  css  js  c++  java
  • Django 练习班级管理系统五 -- 查看老师列表

    models.py 对应的配置

    class Classes(models.Model):
        caption = models.CharField(max_length=32)
    
    class Teacher(models.Model):
        name = models.CharField(max_length=32)
        cls = models.ManyToManyField('Classes')
    
    

    修改 views.py

    @auth
    def handle_teacher(request):
        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())
    
        # 分页,获取第一页5位老师对应的记录
        teacher_list = models.Teacher.objects.filter(id__in=models.Teacher.objects.all()[0:5]).values('id', 'name', 'cls__id', 'cls__caption')
        result = {}
        for t in teacher_list:
            # 判断是否已在 result 中
            if t['id'] in result:
                # 判断 cls__id 的值是否为空
                if t['cls__id']:
                    result[t['id']]['cls_list'].append({'id': t['cls__id'], 'caption': t['cls__caption']})
            else:
                # cls__id 有值
                if t['cls__id']:
                    temp = [{'id': t['cls__id'], 'caption': t['cls__caption']},]
                else:
                    # cls__id 没值
                    temp = []
                # 给 result 字典添加值
                result[t['id']] = {
                    'nid': t['id'],
                    'name': t['name'],
                    'cls_list': temp
                }
    
        return render(request, 'teacher.html', {'username': current_user, 'teacher_list': result})
    
    

    修改 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>
        <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>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endblock %}
    
    {% block js %}
        <script>
            $(function () {
                $('#menu_teacher').addClass('active');
    
            })
        
    
        </script>
    {% endblock %}
    
  • 相关阅读:
    感悟
    业务员如何听懂采购要吃回扣的暗语或者动作?
    回扣应该怎么给——某人的经验
    采购回扣的六种主要方式以及控制方法
    跟曹操学做事
    销售话术,一说就能“套”人,总有一款适合你
    感悟
    创业公司CEO:在公司猝死之前,我先猝死了!
    Python--实例变量
    Python--发送邮件
  • 原文地址:https://www.cnblogs.com/klvchen/p/11137387.html
Copyright © 2011-2022 走看看