老男孩python全栈就业班第9期第3部分Django基础第64天删除作者

删除作者

1.修改项目下目录 templates 的文件 author_list.html,新增删除作者功能的链接

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

<a href="/add_author/">添加新的作者</a>

<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="/delete_author/?id={{ author.id }}">删除</a>
                </td>
            </tr>
        {% endfor %}

    </tbody>
</table>

</body>
</html>

2.修改项目的目录 mysiteday62 下文件 urls.py,新增删除作者的路径

"""mysiteday62 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),

    # 出版社相关的对应关系
    path('publisher_list/', views.publisher_list),
    path('add_publisher/', views.add_publisher),
    path('delete_publisher/', views.delete_publisher),
    path('edit_publisher/', views.edit_publisher),

    # 书相关的对应关系
    path('book_list/', views.book_list),
    path('add_book/', views.add_book), # 添加书籍
    path('delete_book/', views.delete_book), # 删除书籍
    path('edit_book/', views.edit_book), # 编辑书籍

    # 作者相关的对应关系
    path('author_list/', views.author_list), # 展示作者
    path('add_author/', views.add_author), # 添加作者
    path('delete_author/', views.delete_author), # 删除作者

    # 测试
    path('test/', views.test),
]

3.修改项目下目录 app01 下文件 views.py

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

# Create your views here.

# 展示出版社列表
def publisher_list(request):
    # 去数据库查出所有的出版社,填充到HTML中,给用户返回
    ret = models.Publisher.objects.all().order_by("id")
    return render(request, "publisher_list.html", {"publisher_list":ret})

# 添加新的出版社
def add_publisher(request):
    error_msg = ""
    # 如果是POST请求,我就取到用户的数据
    if request.method == "POST":
        new_name = request.POST.get("publisher_name")
        if new_name:
            # 通过ORM去数据库新建一条记录
            models.Publisher.objects.create(name=new_name)
            # 引导用户访问出版社列表,查看是否添加成功,跳转
            return redirect("/publisher_list/")
        else:
            error_msg = "出版社名字不能为空!"
    # 用户第一次来,我给他返回一个用来填写的 HTML 页面
    return render(request, "add_publisher.html", {{"error":error_msg}})

# 删除出版社
def delete_publisher(request):

    # 删除指定的数据
    # 1.从GET请求的参数里面拿到将要删除的数据的ID值
    del_id = request.GET.get("id", None)
    if del_id:
        # 去数据库删除当前id值的数据
        # 根据id值查找当前数据
        del_obj = models.Publisher.objects.get(id=del_id)
        # 删除
        del_obj.delete()
        # 返回删除后的页面,跳转到出版社的列表页,查看删除是否成功
        return redirect("/publisher_list/")
    else:
        return HttpResponse("要删除的数据不存在!")

# 编辑出版社
def edit_publisher(request):
    error_msg = ""
    # 用户修改完出版社的名字,点击提交按钮,给我发来新的出版社名字
    if request.method == "POST":
        # 取新出版社名字
        edit_id = request.POST.get("id")
        new_name = request.POST.get("publisher_name")
        # 更新出版社
        # 根据id取到编辑的是哪个出版社
        edit_publisher = models.Publisher.objects.get(id=edit_id)
        edit_publisher.name = new_name
        edit_publisher.save()  # 把修改提交到数据库
        # 跳转出版社列表页,查看是否修改成功
        return redirect("/publisher_list/")


    # 从GET请求的URL中取到id参数
    edit_id = request.GET.get("id")
    if edit_id:
        # 获取到当前编辑的出版社对象
        publisher_obj  = models.Publisher.objects.get(id=edit_id)
        return render(request, "edit_publisher.html", {"publisher":publisher_obj})
    else:
        return HttpResponse("编辑的出版社不存在!")

# 展示书的列表
def book_list(request):
    # 去数据库中查询所有的书籍
    all_book = models.Book.objects.all()
    # 在 HTML 页面完成字符串替换(渲染数据)
    return render(request, "book_list.html", {"all_book":all_book})

# 删除书籍
def delete_book(request):
    # 从URL里面获取要删除的书记的ID值
    delete_id = request.GET.get("id")  # 从URL里面取数据
    # 去删除数据库中删除指定ID的数据
    models.Book.objects.get(id=delete_id).delete()
    return redirect("/book_list/")

# 添加书籍
def add_book(request):
    if request.method == "POST":
        # {"book_title":"跟金老板学开车", "publisher":7}
        new_title = request.POST.get("book_title")
        new_publisher_id = request.POST.get("publisher")
        # 创建新书对象,自动提交
        models.Book.objects.create(title=new_title, publisher_id=new_publisher_id)
        # 返回书籍列表页
        return redirect("/book_list/")

    # 取到所有的出版社
    ret = models.Publisher.objects.all()
    return render(request, "add_book.html", {"publisher_list":ret})

def edit_book(request):
    if request.method == "POST":
        # 从提交的数据里面取,书名和书关联的出版社
        edit_id = request.POST.get("id")
        new_title = request.POST.get("book_title")
        new_publisher_id = request.POST.get("publisher")
        # 更新
        edit_book_obj = models.Book.objects.get(id=edit_id)
        edit_book_obj.title = new_title # 更新书名
        edit_book_obj.publisher_id = new_publisher_id # 更新书籍关联的出版社
        # 将修改提交到数据库
        edit_book_obj.save()
        # 返回数据列表页面,查看是否编辑成功
        return redirect("/book_list/")


    # 返回一个页面,让用户编辑书籍信息
    # 取到编辑书的id值
    edit_id = request.GET.get("id")
    # 根据id去数据库中把具体的书籍对象拿到
    edit_book_obj = models.Book.objects.get(id=edit_id)
    ret = models.Publisher.objects.all()
    return render(request, "edit_book.html", {'publisher_list':ret, 'book_obj':edit_book_obj})

# 作者列表
def author_list(request):
    author_obj = models.Author.objects.get(id=1)
    print(author_obj.book.all())
    print("*"*120)
    # 查询所有的作者
    all_author = models.Author.objects.all()
    return render(request, "author_list.html", {"author_list":all_author})

# 添加作者
def add_author(request):
    if request.method == "POST":
        print("in post...")
        new_author_name = request.POST.get("author_name")
        #post提交的数据是多个值的时候一定要用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/")
        
    ret = models.Book.objects.all()
    return render(request, "add_author.html", {"book_list":ret})

# 删除作者
def delete_author(request):
    # 从URL里面取到要删除的作者id
    delete_id = request.GET.get("id")
    # 根据ID值取到要删除的作者对象,直接删除
    # 1.去作者表把作者删了
    # 2.去作者和书的关联表,把对应的关联记录删除了
    models.Author.objects.get(id=delete_id).delete()
    # 返回作者列表页面
    return redirect("/author_list/")

def test(request):
    print(request.GET)
    print(request.GET.get("id"))
    return HttpResponse('OK')

4.测试,删除了小绿,并删除了对应关系表

 

posted on 2019-11-26 05:19  herisson_pan  阅读(11)  评论(0)    收藏  举报

导航