1、前端开始跳转,进行 “删除”操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>班级信息</title>
</head>
<body>
<div>
<a href="/add_class/">添加</a>
</div>
<h1>班级列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>班级名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in class_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.title }}</td>
<td><a href="/del_class/?nid={{ row.id }}">删除 </a></td> # 注意这里,并给后端指明要删除信息的id
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
2、在urls.py中声明删除去向
url(r'del_class/', views.del_class),
PS:del_class是后端删除功能函数
3、“删除”功能函数
def del_class(request):
nid = request.GET.get('nid')
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', database='CLASS', charset='utf8')
# 创建游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 执行sql,并返回行数
cursor.execute("delete from class where id=%s", [nid,])
conn.commit()
cursor.close()
conn.close()
return redirect('/classes') # 重定向,更新数据库操作,并刷新页面