部门管理(体验,最原始方法来做,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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/17432.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Docker的容器

Docker的容器 一&#xff0e;容器 容器是一种轻量级的虚拟化技术。它有效的将单个操作系统的资源划分到各独立的组中&#xff0c;以便更好的平衡这些独立的组之间资源的使用。 容器主要包含了命名空间&#xff08;Namespaces&#xff09;和cgroup&#xff08;Control Groups…

[Redis] Redis分布式锁与常见面试题

&#x1f338;个人主页:https://blog.csdn.net/2301_80050796?spm1000.2115.3001.5343 &#x1f3f5;️热门专栏: &#x1f9ca; Java基本语法(97平均质量分)https://blog.csdn.net/2301_80050796/category_12615970.html?spm1001.2014.3001.5482 &#x1f355; Collection与…

Word 公式转 CSDN 插件 发布

经过几个月的苦修&#xff0c;这款插件终于面世了。 从Word复制公式到CSDN粘贴&#xff0c;总是出现公式中的文字被单独提出来&#xff0c;而公式作为一个图片被粘贴的情况。公式多了的时候还会导致CSDN禁止进一步的上传公式。 经过对CSDN公式的研究&#xff0c;发现在粘贴公…

JVM——堆的回收:引用计数发和可达性分析法、五种对象引用

目录 引用计数法和可达性分析法 引用计数法&#xff1a; 可达性分析算法&#xff1a; 五种对象引用 软引用&#xff1a; 弱引用&#xff1a; 引用计数法和可达性分析法 引用计数法&#xff1a; 引用计数法会为每个对象维护一个引用计数器&#xff0c;当对象被引用时加1&…

sqlilabs--小实验

一、先盲注判断 ?id1 and sleep(2)-- 如果发现页面存在注点&#xff0c;使用时间盲注脚本进行注入 import requestsdef inject_database(url):name for i in range(1, 20): # 假设数据库名称长度不超过20low 48 # 0high 122 # zmiddle (low high) // 2while low &l…

VMware Workstate 的 Ubuntu18 安装 vmware tools(不安装没法共享)

在共享主机路径后&#xff0c;可以在&#xff1a; /mnt/hgfs/下方找到共享的文件。但没有安装vmware tool时是没法共享的。 如何安装vmware tool&#xff0c;网上版本很多。这里记录一下&#xff1a; VMware Workstation 17 Pro&#xff0c;版本&#xff1a;17.6.0 虚拟机系统…

STM32 I2C通信协议说明

目录 背景 I2C协议 数据的有效性 I2C通信开始和停止条件 I2C数据传输 发送 响应 正常情况&#xff1a; 异常情况&#xff1a; 主机结束接收 写寄存器的标准流程 读寄存器的标准流程 仲裁机制 时钟同步 SDA线的仲裁 程序 背景 对单片机的三大通信中的I2C通信进…

Unity与SVN集成:实现高效版本控制

内容将会持续更新&#xff0c;有错误的地方欢迎指正&#xff0c;谢谢! Unity与SVN集成&#xff1a;实现高效版本控制 TechX 坚持将创新的科技带给世界&#xff01; 拥有更好的学习体验 —— 不断努力&#xff0c;不断进步&#xff0c;不断探索 TechX —— 心探索、心进取&…

BUU37 [DASCTF X GFCTF 2024|四月开启第一局]web1234【代码审计/序列化/RCE】

Hint1&#xff1a;本题的 flag 不在环境变量中 Hint2&#xff1a;session_start&#xff08;&#xff09;&#xff0c;注意链子挖掘 题目&#xff1a; 扫描出来www.zip class.php <?phpclass Admin{public $Config;public function __construct($Config){//安全获取基…

历史性突破!DeepSeek双模型GitHub热度超OpenAI,展现中国AI力量

在2025年2月7日&#xff0c;中国AI领域传来了一则振奋人心的消息&#xff1a;DeepSeek旗下的两大开源项目在GitHub平台上实现了历史性突破&#xff0c;其Star数成功超越了OpenAI的明星项目。这一成就不仅标志着DeepSeek在技术研发和市场影响力上的重大飞跃&#xff0c;也为中国…

肝了半年,我整理出了这篇云计算学习路线(新手必备,从入门到精通)

大家好&#xff01;我是凯哥&#xff0c;今天给大家分享一下云计算学习路线图。这是我按照自己最开始学习云计算的时候的学习路线&#xff0c;并且结合自己从业多年所涉及的知识精心总结的云计算的思维导图。这是凯哥精心总结的&#xff0c;花费了不少精力哦&#xff0c;希望对…

畅聊deepseek-r1,SiliconFlow 硅基流动注册+使用

文章目录 SiliconFlow 硅基流动注册使用注册创建API密钥使用网页端使用代码调用api调用支持的模型 SiliconFlow 硅基流动注册使用 注册 硅基流动官网 https://cloud.siliconflow.cn/i/XcgtUixn 注册流程 切换中文 ​ 邀请码&#xff1a; XcgtUixn 创建API密钥 账户管理 --&g…

Java 大数据与区块链的融合:数据可信共享与溯源(45)

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

初阶c语言(循环语句习题,完结)

前言&#xff1a; c语言为b站鹏哥&#xff0c;嗯对应视频37集 昨天做的c语言&#xff0c;今天在来做一遍&#xff0c;发现做错了 今天改了平均值的计算&#xff0c; 就是说最大值加上最小值&#xff0c;如果说这个数值非常大的话&#xff0c;两个值加上会超过int类型的最大…

传感器篇(一)——深度相机

目录 一 概要 二 原理 三 对比 四 产品 五 结论 一 概要 深度相机是一种能够获取物体深度信息的设备&#xff0c;相较于普通相机只能记录物体的二维图像信息&#xff0c;深度相机可以感知物体与相机之间的距离&#xff0c;从而提供三维空间信息。在你正在阅读的报告中提到…

蓝桥杯之并查集

算法思想 并查集是一种树形的数据结构&#xff0c;主要用于解决一些元素分组问题。用于处理一些不相交集合的合并以及查询问题。并查集的思想是用一个数组表示了整片森林&#xff0c;树的根节点唯一标识了一个集合&#xff0c;我们只要找到了某个元素的树根&#xff0c;就能确…

Windows11+PyCharm利用MMSegmentation训练自己的数据集保姆级教程

系统版本&#xff1a;Windows 11 依赖环境&#xff1a;Anaconda3 运行软件&#xff1a;PyCharm 一.环境配置 通过Anaconda Prompt(anaconda)打开终端创建一个虚拟环境 conda create --name mmseg python3.93.激活虚拟环境 conda activate mmseg 4.安装pytorch和cuda tor…

#渗透测试#批量漏洞挖掘#Crocus系统—Download 文件读取

免责声明 本教程仅为合法的教学目的而准备&#xff0c;严禁用于任何形式的违法犯罪活动及其他商业行为&#xff0c;在使用本教程前&#xff0c;您应确保该行为符合当地的法律法规&#xff0c;继续阅读即表示您需自行承担所有操作的后果&#xff0c;如有异议&#xff0c;请立即停…

day09_实时类标签/指标

文章目录 day09_实时类标签/指标一、日志数据实时采集2、Flume简介2.3 项目日志数据采集Flume配置2.3.1 涉及的Flume组件和参数2.3.2 Nginx日志采集2.3.3 用户行为日志采集 二、Nginx日志数据统计1、日志格式说明2、数据ETL2.1 日志抽取2.1.1 正则表达式2.1.2 基于Spark实现Ngi…

SpringBoot实战:高效获取视频资源

文章目录 前言技术实现SpringBoot项目构建产品选取配置数据采集 号外号外 前言 在短视频行业高速发展的背景下&#xff0c;海量内容数据日益增长&#xff0c;每天都有新的视频、评论、点赞、分享等数据涌现。如何高效、精准地获取并处理这些庞大的数据&#xff0c;已成为各大平…