当前位置: 首页 > news >正文

部门管理(体验,最原始方法来做,Django+mysql)

本人初学,写完代码在此记录和复盘 

在创建和注册完APP之后(我的命名是employees),编写models.py文件创建表

 手动插入了几条数据

 

1.部门查询

urls.py和views.py在编写之前,都要注意导入对应的库

urls.py:from employees import views    (用于导入视图函数)

views.py:from employees import models    (用于导入表)

def depart_list(request):# 从数据库中获取所有部门信息,返回一个查询集queryset = models.Department.objects.all()# 渲染 'depart_list.html' 模板,传递查询集数据到模板中return render(request, 'depart_list.html', {"queryset": queryset})

在templates文件夹中创建对应的html文件 

depart_list.html

{% load static %}<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="{% static 'plugins/bootstrap-5.3.3/css/bootstrap.css' %}"><link rel="stylesheet" href="{% static 'plugins/font-awesome-4.7.0/css/font-awesome.css' %}"><style>.c1{margin-top: 20px;margin-bottom: 20px;}</style></head>
<body><nav class="navbar navbar-expand-lg bg-light"><div class="container"><a class="navbar-brand" href="#">部门管理系统</a><button class="navbar-toggler" type="button" data-bs-toggle="collapse"data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navbarSupportedContent"><ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item"><a class="nav-link active" aria-current="page" href="/depart/list/">部门首页</a></li><li class="nav-item"><a class="nav-link" href="/depart/add">部门管理</a></li><form class="d-flex"><input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"><button class="btn btn-outline-success" type="submit">Search</button></form></ul></div><ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item"><a class="nav-link active" aria-current="page" href="#">登录</a></li><li class="nav-item"><a class="nav-link" href="#">注册</a></li><li class="nav-item dropdown"><a class="nav-link dropdown-toggle" href="#" id="log" role="button"data-bs-toggle="dropdown" aria-expanded="false">豆腐乳</a><ul class="dropdown-menu" aria-labelledby="navbarDropdown"><li><a class="dropdown-item" href="#">个人资料</a></li><li><a class="dropdown-item" href="#">我的账户</a></li><li><a class="dropdown-item" href="#">修改密码</a></li><li><hr class="dropdown-divider"></li><li><a class="dropdown-item" href="#">注销</a></li></ul></li></ul></div>
</nav><div><div class="container"><div class="c1"><a class="btn btn-success" href="/depart/add">新建部门</a></div><div class="card c1"><div class="card-header"><i class="fa fa-list" aria-hidden="true"></i>部门列表</div><div class="card-body p-0"><div class="table-responsive"><table class="table table-striped table-bordered table-hover mb-0"><thead><tr><th>ID</th><th>名称</th><th>操作</th></tr></thead><tbody>{% for obj in queryset %}<tr><th scope="row">{{ obj.id }}</th><td>{{ obj.title }}</td><td><a class="btn btn-primary btn-xs" href="/depart/{{ obj.id }}/edit/">编辑</a><a class="btn btn-danger btn-xs" href="/depart/delete/?nid={{ obj.id }}">删除</a></td></tr>{% endfor %}</tbody></table></div></div></div><nav aria-label="Page navigation example"><ul class="pagination"><li class="page-item"><a class="page-link" href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li><li class="page-item"><a class="page-link" href="#">1</a></li><li class="page-item"><a class="page-link" href="#">2</a></li><li class="page-item"><a class="page-link" href="#">3</a></li><li class="page-item"><a class="page-link" href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li></ul></nav></div>
</div><script src="{% static 'plugins/bootstrap-5.3.3/js/bootstrap.bundle.min.js' %}"></script></body>
</html>
  1. 视图函数 depart_list
    查询数据库中的部门表(Department),将所有部门数据传递给模板 depart_list.html

  2. 模板循环
    遍历 queryset 中的每个部门对象,生成表格行,显示部门 ID、名称,并提供“编辑”和“删除”操作链接。

    • 编辑:跳转至 /depart/{id}/edit/

    • 删除:跳转至 /depart/delete/?nid={id}

2.部门增加

def depart_add(request):# 处理 GET 请求:返回添加部门的页面if request.method == 'GET':return render(request, 'depart_add.html')# 处理 POST 请求:获取表单中的部门名称并保存到数据库title = request.POST.get("title")  # 从 POST 请求中获取部门名称models.Department.objects.create(title=title)  # 创建新部门# 添加成功后,重定向到部门列表页面return redirect("/depart/list")

depart_list.html

{% load static %}<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="{% static 'plugins/bootstrap-5.3.3/css/bootstrap.css' %}"><link rel="stylesheet" href="{% static 'plugins/font-awesome-4.7.0/css/font-awesome.css' %}"><style>.c1{margin-top: 20px;margin-bottom: 20px;}</style></head>
<body><nav class="navbar navbar-expand-lg bg-light"><div class="container"><a class="navbar-brand" href="#">部门管理系统</a><button class="navbar-toggler" type="button" data-bs-toggle="collapse"data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navbarSupportedContent"><ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item"><a class="nav-link active" aria-current="page" href="/depart/list/">部门首页</a></li><li class="nav-item"><a class="nav-link" href="/depart/add">部门管理</a></li><form class="d-flex"><input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"><button class="btn btn-outline-success" type="submit">Search</button></form></ul></div><ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item"><a class="nav-link active" aria-current="page" href="#">登录</a></li><li class="nav-item"><a class="nav-link" href="#">注册</a></li><li class="nav-item dropdown"><a class="nav-link dropdown-toggle" href="#" id="log" role="button"data-bs-toggle="dropdown" aria-expanded="false">豆腐乳</a><ul class="dropdown-menu" aria-labelledby="navbarDropdown"><li><a class="dropdown-item" href="#">个人资料</a></li><li><a class="dropdown-item" href="#">我的账户</a></li><li><a class="dropdown-item" href="#">修改密码</a></li><li><hr class="dropdown-divider"></li><li><a class="dropdown-item" href="#">注销</a></li></ul></li></ul></div>
</nav><div><div class="container"><div class="c1"><a class="btn btn-success" href="/depart/add">新建部门</a></div><div class="card c1"><div class="card-header">新建部门</div><div class="card-body"><form method="post">{% csrf_token %}<div class="mb-3"><label for="inputtitle" class="form-label">标题</label><input type="text" class="form-control" id="inputtitle" placeholder="标题" name="title"></div><button type="submit" class="btn btn-primary">提交</button></form></div></div></div>
</div><script src="{% static 'plugins/bootstrap-5.3.3/js/bootstrap.bundle.min.js' %}"></script></body>
</html>

运行

 

3.部门删除

def depart_delete(request):# 获取请求中传递的 'nid' 参数,即要删除的部门 IDnid = request.GET.get("nid")# 根据 'nid' 参数过滤部门对象并删除models.Department.objects.filter(id=nid).delete()# 删除完成后,重定向回部门列表页面return redirect("/depart/list")

  depart_delete 函数效果:运行/depart/delete/?nid=1,就会删除id为1的数据

当用户点击“删除”按钮时,会触发 depart_delete 函数,该函数根据部门的 ID 删除对应的部门数据,并重定向回部门列表页面。

运行

 

 

4.部门修改

<int:nid>:这是一个捕获组,它告诉 Django 该部分的路径将匹配一个整数,并将这个整数作为参数传递给视图函数。在这里,nid 是变量名,表示部门的 ID。 

def depart_edit(request, nid):# 如果请求方法是 GET(即用户访问编辑页面),获取对应部门的信息if request.method == 'GET':# 根据部门 ID 获取部门对象,并将其传递给模板row_object = models.Department.objects.filter(id=nid).first()return render(request, 'depart_edit.html', {"row_object": row_object})# 如果请求方法是 POST(即用户提交表单),更新部门信息title = request.POST.get("title")# 更新数据库中对应部门的标题models.Department.objects.filter(id=nid).update(title=title)# 更新完成后,重定向到部门列表页面return redirect("/depart/list")

当用户访问 /depart/<nid>/edit/ URL 时,depart_edit 函数会根据部门 ID (nid) 获取该部门的现有信息并显示在表单中,用户可以修改部门名称并提交表单。提交表单后,depart_edit 函数会更新数据库中对应部门的标题,并将用户重定向回部门列表页面。

depart_edit.py

{% load static %}<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="{% static 'plugins/bootstrap-5.3.3/css/bootstrap.css' %}"><link rel="stylesheet" href="{% static 'plugins/font-awesome-4.7.0/css/font-awesome.css' %}"><style>.c1{margin-top: 20px;margin-bottom: 20px;}</style></head>
<body><nav class="navbar navbar-expand-lg bg-light"><div class="container"><a class="navbar-brand" href="#">部门管理系统</a><button class="navbar-toggler" type="button" data-bs-toggle="collapse"data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navbarSupportedContent"><ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item"><a class="nav-link active" aria-current="page" href="/depart/list/">部门首页</a></li><li class="nav-item"><a class="nav-link" href="/depart/add">部门管理</a></li><form class="d-flex"><input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"><button class="btn btn-outline-success" type="submit">Search</button></form></ul></div><ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item"><a class="nav-link active" aria-current="page" href="#">登录</a></li><li class="nav-item"><a class="nav-link" href="#">注册</a></li><li class="nav-item dropdown"><a class="nav-link dropdown-toggle" href="#" id="log" role="button"data-bs-toggle="dropdown" aria-expanded="false">豆腐乳</a><ul class="dropdown-menu" aria-labelledby="navbarDropdown"><li><a class="dropdown-item" href="#">个人资料</a></li><li><a class="dropdown-item" href="#">我的账户</a></li><li><a class="dropdown-item" href="#">修改密码</a></li><li><hr class="dropdown-divider"></li><li><a class="dropdown-item" href="#">注销</a></li></ul></li></ul></div>
</nav><div><div class="container"><div class="card c1"><div class="card-header">修改部门</div><div class="card-body"><form method="post">{% csrf_token %}<div class="mb-3"><label for="inputtitle" class="form-label">标题</label><input type="text" class="form-control" id="inputtitle" placeholder="标题" name="title" value="{{ row_object.title }}"/></div><button type="submit" class="btn btn-primary">提交</button></form></div></div></div>
</div><script src="{% static 'plugins/bootstrap-5.3.3/js/bootstrap.bundle.min.js' %}"></script></body>
</html>

运行

改成“媒体” 

 

 

 学习:【最新Python的web开发全家桶(django+前端+数据库)-哔哩哔哩】 https://b23.tv/O0w3SND

相关文章:

部门管理(体验,最原始方法来做,Django+mysql)

本人初学&#xff0c;写完代码在此记录和复盘 在创建和注册完APP之后&#xff08;我的命名是employees&#xff09;&#xff0c;编写models.py文件创建表 手动插入了几条数据 1.部门查询 urls.py和views.py在编写之前&#xff0c;都要注意导入对应的库 urls.py&#xff1a;…...

clickhouse集群搭建

Clickhouse集群搭建 文章目录 Clickhouse集群搭建安装包下载clickhouse单机安装默认安装默认数据库目录更改默认数据目录 2分片-1副本-3节点集群搭建1. 配置hosts2. 修改每个主机的主机名3. 配置文件上传配置文件分布chnode1配置文件chnode2配置文件chnode3配置文件 4. 重启cli…...

250214-java类集框架

单列集合是list和set&#xff0c;list的实现类有ArrayList和LinkedList&#xff0c;前者是数组实现&#xff0c;后者是链表实现。list和set&#xff0c;前者有序、可重复&#xff0c;后者无序不可重复。 1.单列集合 1.1. list java.util.List接口继承自Collection接口&#…...

二叉树(C语言版)

文章目录 二叉树完全二叉树和满二叉树二叉搜索树基本操作实现代码运行结果 分析红黑树2-3-4树(理论模型)红黑树(实际实现) 二叉树 树是一种层次结构&#xff0c;它在现实生活中是广泛存在的&#xff0c;比如&#xff1a;族谱(family tree)&#xff0c;组织机构&#xff0c;目录…...

ASP.NET Core 面试宝典【刷题系列】

文章目录 引言1、什么是 dot net core 的 startup class?2、什么是中间件?3、application builder 的 use 和 run 方法有什么区别?4、dot net core 管道里面的map拓展有什么作用?5、dot net core 里面的路径是如何处理的?6、如何在 dot net core 中激活 session 功能?7、…...

案例-02.部门管理-查询

一.查询部门-需求 二.查询部门-思路 API接口文档 三.代码实现 1.controller层&#xff1a;负责与前端进行交互&#xff0c;接收前端所发来的请求 注&#xff1a;Slf4j用于记录日志使用&#xff0c;可以省略private static Logger log LoggerFactory.getLogger(DeptControlle…...

src和href区别

src和href区别 (1)请求资源类型不同(2)作用结果不同(3)解析方式不同 (1)请求资源类型不同 href 用来建立文档和元素之间的链接(是引用),常用的有a、linksrc 在请求src资源时候会将指向的资源下载并且应用到文档中(引入),常用的有script、iframe、image。 (2)作用结果不同 hr…...

Java每日精进·45天挑战·Day19

第一部分&#xff1a;移除数字以形成最小数的贪心算法实现 在编程的世界里&#xff0c;我们经常遇到需要对字符串表示的数字进行操作的问题。今天&#xff0c;我们要深入探讨一个具体的挑战&#xff1a;给定一个以字符串形式表示的非负整数 num 和一个整数 k&#xff0c;我们的…...

区块链的交易管理和共识机制

区块链的交易管理和共识机制是其核心功能&#xff0c;以下为你详细介绍它们的实现方式&#xff1a; 交易管理的实现 交易发起 • 用户使用钱包软件创建一笔交易&#xff0c;该交易包含发送方地址、接收方地址、转账金额等关键信息。同时&#xff0c;发送方会使用自己的私钥对…...

最新国内 ChatGPT Plus/Pro 获取教程

最后更新版本&#xff1a;20250202 教程介绍&#xff1a; 本文将详细介绍如何快速获取一张虚拟信用卡&#xff0c;并通过该卡来获取ChatGPT Plus和ChatGPT Pro。 # 教程全程约15分钟开通ChatGPT Plus会员帐号前准备工作 一个尚未升级的ChatGPT帐号&#xff01;一张虚拟信用卡…...

Apollo 9.0 速度动态规划决策算法 – path time heuristic optimizer

文章目录 1. 动态规划2. 采样3. 代价函数3.1 障碍物代价3.2 距离终点代价3.3 速度代价3.4 加速度代价3.5 jerk代价 4. 回溯 这一章将来讲解速度决策算法&#xff0c;也就是SPEED_HEURISTIC_OPTIMIZER task里面的内容。Apollo 9.0使用动态规划算法进行速度决策&#xff0c;从类名…...

Apache Iceberg 与 Apache Hudi:数据湖领域的双雄对决

在数据存储和处理不断发展的领域中&#xff0c;数据湖仓的概念已经崭露头角&#xff0c;成为了一种变革性的力量。数据湖仓结合了数据仓库和数据湖的最佳元素&#xff0c;提供了一个统一的平台&#xff0c;支持数据科学、商业智能、人工智能/机器学习以及临时报告等多种关键功能…...

【LeetCode Hot100 普通数组】最大子数组和、合并区间、旋转数组、除自身以外数组的乘积、缺失的第一个正整数

普通数组 1. 最大子数组和&#xff08;Maximum Subarray&#xff09;解题思路动态规划的优化解法&#xff08;Kadane 算法&#xff09;核心思想 代码解析 2. 合并区间&#xff08;Merge Intervals&#xff09;解题思路代码实现 3. 旋转数组&#xff08;Rotate Array&#xff09…...

共享存储-一步一步部署ceph分布式文件系统

一、Ceph 简介 Ceph 是一个开源的分布式文件系统。因为它还支持块存储、对象存储&#xff0c;所以很自 然的被用做云计算框架 openstack 或 cloudstack 整个存储后端。当然也可以单独作 为存储&#xff0c;例如部署一套集群作为对象存储、SAN 存储、NAS 存储等。 二、ceph 支…...

19.Python实战:实现对博客文章的点赞系统

Flask博客点赞系统 一个基于Flask的简单博客系统&#xff0c;具有文章展示和点赞功能。系统使用MySQL存储数据&#xff0c;支持文章展示、点赞/取消点赞等功能。 功能特点 文章列表展示文章详情查看&#xff08;模态框展示&#xff09;点赞/取消点赞功能&#xff08;每个IP只…...

【stm32】定时器输出PWM波形(hal库)

一. PWM基本原理 PWM是一种通过调节信号的占空比&#xff08;Duty Cycle&#xff09;来控制输出平均电压的技术。占空比是指高电平时间与整个周期时间的比值。例如&#xff1a; - 占空比为50%时&#xff0c;输出平均电压为电源电压的一半。 - 占空比为100%时&#xff0c;输出始…...

当Ollama遇上划词翻译:我的Windows本地AI服务搭建日记

&#x1f680; 实现Windows本地大模型翻译服务 - 基于OllamaFlask的划词翻译实践 &#x1f6e0;️ 步骤概要1️⃣ python 环境准备2️⃣ Ollama 安装3️⃣ 一个 Flask 服务4️⃣ Windows 服务化封装5️⃣ 测试本地接口6️⃣ 配置划词翻译自定义翻译源7️⃣ 效果展示8️⃣ debug…...

Linux上Elasticsearch 集群部署指南

Es 集群部署 Es 集群部署 Es 集群部署 准备好三台服务器。示例使用&#xff1a;110.0.5.141/142/143 1、es用户和用户组创建&#xff0c;使用root账号 groupadd esuseradd -g es es2、将es安装包和ik分词器上传到&#xff1a;/home/es/目录下&#xff08;任意目录都行&#…...

字节Trae使用感想(后端)

前言 昨天分享了字节哥的Trae从0到1创作模式构建一个vue前端项目&#xff0c;今天又来试试她的后端项目能力。不是我舔&#xff0c;不得不说确实不错。可惜现在曾经没有好好学习&#xff0c;进不了字节。既然进不了字节&#xff0c;那我就用字节哥的产品吧。 后面有惊喜…...

国产编辑器EverEdit - 二进制模式下观察Window/Linux/MacOs换行符差异

1 换行符格式 1.1 应用场景 稍微了解计算机历史的人都知道&#xff0c; 计算机3大操作系统&#xff1a; Windows、Linux/Unix、MacOS&#xff0c;这3大系统对文本换行的定义各不相同&#xff0c;且互不相让&#xff0c;导致在文件的兼容性方面存在一些问题&#xff0c;比如它们…...

7.4.分块查找

一.分块查找的算法思想&#xff1a; 1.实例&#xff1a; 以上述图片的顺序表为例&#xff0c; 该顺序表的数据元素从整体来看是乱序的&#xff0c;但如果把这些数据元素分成一块一块的小区间&#xff0c; 第一个区间[0,1]索引上的数据元素都是小于等于10的&#xff0c; 第二…...

Cursor实现用excel数据填充word模版的方法

cursor主页&#xff1a;https://www.cursor.com/ 任务目标&#xff1a;把excel格式的数据里的单元格&#xff0c;按照某一个固定模版填充到word中 文章目录 注意事项逐步生成程序1. 确定格式2. 调试程序 注意事项 直接给一个excel文件和最终呈现的word文件的示例&#xff0c;…...

云启出海,智联未来|阿里云网络「企业出海」系列客户沙龙上海站圆满落地

借阿里云中企出海大会的东风&#xff0c;以**「云启出海&#xff0c;智联未来&#xff5c;打造安全可靠的出海云网络引擎」为主题的阿里云企业出海客户沙龙云网络&安全专场于5.28日下午在上海顺利举办&#xff0c;现场吸引了来自携程、小红书、米哈游、哔哩哔哩、波克城市、…...

Debian系统简介

目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版&#xff…...

屋顶变身“发电站” ,中天合创屋面分布式光伏发电项目顺利并网!

5月28日&#xff0c;中天合创屋面分布式光伏发电项目顺利并网发电&#xff0c;该项目位于内蒙古自治区鄂尔多斯市乌审旗&#xff0c;项目利用中天合创聚乙烯、聚丙烯仓库屋面作为场地建设光伏电站&#xff0c;总装机容量为9.96MWp。 项目投运后&#xff0c;每年可节约标煤3670…...

12.找到字符串中所有字母异位词

&#x1f9e0; 题目解析 题目描述&#xff1a; 给定两个字符串 s 和 p&#xff0c;找出 s 中所有 p 的字母异位词的起始索引。 返回的答案以数组形式表示。 字母异位词定义&#xff1a; 若两个字符串包含的字符种类和出现次数完全相同&#xff0c;顺序无所谓&#xff0c;则互为…...

深入解析C++中的extern关键字:跨文件共享变量与函数的终极指南

&#x1f680; C extern 关键字深度解析&#xff1a;跨文件编程的终极指南 &#x1f4c5; 更新时间&#xff1a;2025年6月5日 &#x1f3f7;️ 标签&#xff1a;C | extern关键字 | 多文件编程 | 链接与声明 | 现代C 文章目录 前言&#x1f525;一、extern 是什么&#xff1f;&…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

企业如何增强终端安全?

在数字化转型加速的今天&#xff0c;企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机&#xff0c;到工厂里的物联网设备、智能传感器&#xff0c;这些终端构成了企业与外部世界连接的 “神经末梢”。然而&#xff0c;随着远程办公的常态化和设备接入的爆炸式…...