05已经实现了部门修改的功能,下面我们开始实现如何去删除一个部门
新增删除入口
html
{% extends 'base.html' %}
{% block content %}
<div class="container-fluid">
<div style="margin-bottom: 10px" class="clearfix">
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">
<a class="btn btn-primary" href="/dept/add/" role="button">添加部门</a>
</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>
<th>操作</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>
<td>
<a class="btn btn-success" href="/dept/{{ obj.id }}/edit_detail/" role="button">编辑部门</a>
<a class="btn btn-danger" href="/dept/{{ obj.id }}/delete/" role="button">删除部门</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}
配置URL路径
python
urlpatterns = [
# 部门管理
path("dept/list/", dept.dept_list),
path("dept/add/", dept.dept_add),
path("dept/<int:nid>/edit_detail/", dept.dept_editdetail),
path("dept/<int:nid>/delete/", dept.dept_delete),
]
然后去dept.py中去定义函数dept_delete()
python
"""删除部门"""
def dept_delete(request, nid):
# 根据id,获取到该行数据
row_obj = models.Dept.objects.using('default').filter(id=nid)
# 删除
row_obj.delete()
return redirect('/dept/list')
我们尝试把6病区沙和尚删掉
可以看到,删除成功了。
删除功能还是比较简单的。<(* ̄▽ ̄*)/