修改 author_list.html,添加删除按钮

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>作者列表</title>
</head>
<body>

<h1>作者列表</h1>

<table border="1">
    <thead>
    <tr>
        <th>#</th>
        <th>id</th>
        <th>名字</th>
        <th>书籍</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {% for author in author_list %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ author.id }}</td>
            <td>{{ author.name }}</td>
            <td>
                {% for book in author.book.all %}
                    {% if forloop.last %}
                        {{ book.title }}
                    {% else %}
                        {{ book.title }} |
                    {% endif %}
                {% endfor %}
            </td>

            <td>
                <a href="/del_author/?id={{ author.id }}">删除</a>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>

<a href="/add_author/">添加书籍</a>

</body>
</html>

运行效果:

Python - Django - 删除作者

在 urls.py 中添加 del_author 的对应关系

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    # 出版社
    url(r'^publisher_list/', views.publisher_list),
    url(r'^add_publisher/', views.add_publisher),
    url(r'^del_publisher/', views.del_publisher),
    url(r'^edit_publisher/', views.edit_publisher),
    # 书籍
    url(r'^book_list/', views.book_list),
    url(r'^add_book/', views.add_book),
    url(r'^del_book/', views.del_book),
    url(r'^edit_book/', views.edit_book),
    # 作者
    url(r'^author_list/', views.author_list),
    url(r'^add_author/', views.add_author),
    url(r'^del_author/', views.del_author),
]

在 views.py 中添加 del_author 函数

from django.shortcuts import render, redirect, HttpResponse
from app01 import models


# 展示出版社列表
def publisher_list(request):
    pass


# 添加新的出版社
def add_publisher(request):
    pass


# 删除出版社
def del_publisher(request):
    pass


# 编辑出版社
def edit_publisher(request):
    pass


# 展示书籍列表
def book_list(request):
    pass


# 添加书籍
def add_book(request):
    pass


# 删除书籍
def del_book(request):
    pass


# 编辑书籍
def edit_book(request):
    pass


# 作者列表
def author_list(request):
    # 查询所有作者
    all_author = models.Author.objects.all()
    return render(request, "author_list.html", {"author_list": all_author})


# 添加作者
def add_author(request):
    if request.method == "POST":
        # 取得提交的数据
        new_author_name = request.POST.get("author_name")
        # 如果提交的数据是多个值的话用 getlist
        books = request.POST.getlist("books")
        # 创建作者
        new_author_obj = models.Author.objects.create(name=new_author_name)
        # 把新作者和书籍建立对应关系,自动提交
        new_author_obj.book.set(books)
        # 跳转到作者列表页面,查看是否添加成功
        return redirect("/author_list/")
    # 查询所有的书籍
    all_books = models.Book.objects.all()
    return render(request, "add_author.html", {"book_list": all_books})


# 删除作者
def del_author(request):
    # 从 url 里提取要删除的作者 id
    del_id = request.GET.get("id")
    # 根据 id 删除 author 表和其相关联表的数据
    models.Author.objects.get(id=del_id).delete()
    # 返回作者列表
    return redirect("author_list.html")

删除 Jane 这一行

Python - Django - 删除作者

看一下数据库

Python - Django - 删除作者

Python - Django - 删除作者