03 django管理系统 - 部门管理 - 部门列表

部门管理

首先我们需要在models里定义Dept类

python 复制代码
# 创建部门表
class Dept(models.Model):
    name = models.CharField(max_length=100)
    head = models.CharField(max_length=100)
    phone = models.CharField(max_length=15)
    email = models.EmailField()
    address = models.CharField(max_length=255)

    def __str__(self):
        return self.name

然后执行下面两句,创建数据表

manage.py@MS > makemigrations

manage.py@MS > migrate

部门列表展示

首先创建视图

python 复制代码
from django.shortcuts import render

"""部门列表"""
def dept_list(request):
    return render(request, 'dept_list.html')

然后配置URL路由

python 复制代码
urlpatterns = [
    # 部门管理
    path("dept/list/", dept.dept_list),
]

接着创建部门列表页面dept_list.html,继承base.html

python 复制代码
{% extends 'base.html' %}

{% block content %}
    
{% endblock %}

看一下效果,是可以访问的。

去bootstrap找个带表格的面板,加进去

html 复制代码
{% extends 'base.html' %}

{% block content %}

    <div class="panel panel-default">
        <!-- Default panel contents -->
        <div class="panel-heading">新建部门</div>
        <div class="panel-body">
            <p>部门列表</p>
        </div>

        <!-- Table -->
        <table class="table">
            <thead>
            <tr>
                <th>#</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Username</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <th scope="row">1</th>
                <td>Mark</td>
                <td>Otto</td>
                <td>@mdo</td>
            </tr>
            <tr>
                <th scope="row">2</th>
                <td>Jacob</td>
                <td>Thornton</td>
                <td>@fat</td>
            </tr>
            <tr>
                <th scope="row">3</th>
                <td>Larry</td>
                <td>the Bird</td>
                <td>@twitter</td>
            </tr>
            </tbody>
        </table>
    </div>

{% endblock %}

效果如下:

ok,接下来可以编写dept_list()函数

python 复制代码
"""
部门列表
department_id:部门ID(主键,自动增长)
name:部门名称
head:部门负责人
phone:联系电话
email:电子邮件
address:地址
"""


def dept_list(request):
    # 查询部门列表数据
    queryset = models.Dept.objects.using('default').all()
    # 把查询到的数据传递到前端
    context = {
        'queryset': queryset
    }
    return render(request, 'dept_list.html', context)

然后在dept_list.html上接收数据

html 复制代码
{% extends 'base.html' %}

{% block content %}

    <div class="panel panel-default">
        <!-- Default panel contents -->
        <div class="panel-heading">新建部门</div>
        <div class="panel-body">
            <p>部门列表</p>
        </div>

        <!-- Table -->
        <table class="table">
            <thead>
            <tr>
                <th>ID</th>
                <th>name</th>
                <th>head</th>
                <th>phone</th>
                <th>email</th>
                <th>address</th>
            </tr>
            </thead>
            <tbody>
            {% for obj in queryset %}
                <tr>
                    <td>{{ obj.id }}</td>
                    <td>{{ obj.name }}</td>
                    <td>{{ obj.head }}</td>
                    <td>{{ obj.phone }}</td>
                    <td>{{ obj.email }}</td>
                    <td>{{ obj.address }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

{% endblock %}

效果如下:

相关推荐
Livingbody7 分钟前
通过huggingface学习【自动语音识别(Automatic Speech Recognition, ASR)】
后端
~plus~10 分钟前
Harmony核心:动态方法修补与.NET游戏Mod开发
开发语言·jvm·经验分享·后端·程序人生·c#
~plus~20 分钟前
WPF八大法则:告别模态窗口卡顿
开发语言·经验分享·后端·程序人生·c#
Livingbody25 分钟前
Transformers Pipeline 入门之【任务列表】
后端
[email protected]32 分钟前
ASP.NET Core SignalR - 部分客户端消息发送
后端·asp.net·.netcore
寻月隐君32 分钟前
深入解析 Rust 的面向对象编程:特性、实现与设计模式
后端·rust·github
追逐时光者36 分钟前
免费且全面的C#/.NET/.NET Core面试宝典,阅读量突破40万+了!
后端·.net
程序员三藏1 小时前
如何使用Jmeter进行压力测试?
自动化测试·软件测试·python·测试工具·jmeter·测试用例·压力测试
carpell1 小时前
【语义分割专栏】3:Segnet原理篇
人工智能·python·深度学习·计算机视觉·语义分割
24K纯学渣1 小时前
Python编码格式化之PEP8编码规范
开发语言·ide·python·pycharm