• 开发图书增删改查页面


    开发图书增删改查页面
    需求:
    1.列出图书列表、出版社列表、作者列表
    2.点击作者,会列出其出版的图书列表
    3.点击出版社,会列出旗下图书列表
    4.可以创建、修改、删除 图书、作者、出版社

    主页信息:
    http://127.0.0.1:8000/index/

    model.py
    from django.db import models
    
    # Create your models here.
    
    class Book(models.Model):
        """书表"""
        id = models.AutoField(primary_key=True)
        title = models.CharField(max_length=32)
        publish_date = models.DateField()
        publisher = models.ForeignKey("Publisher",on_delete=models.CASCADE)
        author = models.ManyToManyField("Author")
    
    class Publisher(models.Model):
         '''出版社表'''
         id = models.AutoField(primary_key=True)
         pname = models.CharField(max_length=32)
    
    class Author(models.Model):
         '''作者表'''
         id = models.AutoField(primary_key=True)
         aname = models.CharField(max_length=32)

    urls.py

    
    
    from django.contrib import admin
    from django.urls import path, re_path
    
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('index/', views.index),
    
        path('book/', views.book),
        path('addbook/', views.addbook),
        re_path(r"^delbook/", views.delbook), #delbook(request,2)
        re_path(r"^editbook/", views.editbook),
        #re_path(r"query",views.query),
    
        re_path(r'^publisher', views.publisher),
        re_path(r'^addpublisher', views.addpublisher),
        re_path(r'^delpublisher', views.delpublisher),
        re_path(r'^editpublisher', views.editpublisher),
        re_path(r'^publisher_book', views.publisher_book),
    
        re_path(r'^author/', views.author),
        re_path(r'^addauthor', views.addauthor),
        re_path(r'^delauthor', views.delauthor),
        re_path(r'^editauthor', views.editauthor),
        re_path(r'^author_book', views.author_book),
    
    ]
    
    

    views.py

    from django.shortcuts import render, HttpResponse, redirect
    
    # Create your views here.
    from app01.models import Book
    from app01 import models
    
    #主页
    def index(request):
        return render(request,"index.html")
    
    #添加书籍
    def addbook(request):
        if request.method == 'POST':
            title = request.POST.get("title")
            pid = request.POST.get("pid")
            publish_date = request.POST.get("pub_date")
            authors = request.POST.getlist("aids")
    
            models.Book.objects.create(title=title,publish_date=publish_date,publisher_id=pid)
            new_book = models.Book.objects.get(title=title)
            new_book.author.set(authors)
            return redirect("/book/")
        pub_list = models.Publisher.objects.all()
        author_list = models.Author.objects.all()
        return render(request, "addbook.html", {"pub_list": pub_list, "author_list": author_list})
    
    #查看图书
    def book(request):
        book_list = Book.objects.all()
        return render(request, "book.html", {"book_list":book_list})
    #删除书籍
    def delbook(request):
        bid = request.GET.get("bid")
        book_obj = models.Book.objects.filter(id=bid).first()
        book_obj.delete()
        return redirect("/book/")
    #编辑书籍
    def editbook(request):
        bid = request.GET.get("bid")
        book_obj = models.Book.objects.filter(id=bid).first()
        if request.method == "POST":
            title = request.POST.get("title")
            pid = request.POST.get("pid")
            publish_date = request.POST.get("pub_date")
            authors = request.POST.getlist("aids")
            models.Book.objects.filter(id=bid).update(title=title, publish_date=publish_date, publisher_id=pid)
            edit_book = models.Book.objects.get(id=bid)
            edit_book.author.set(authors)
            return redirect("/book/")
        pub_list = models.Publisher.objects.all()
        author_list = models.Author.objects.all()
        return render(request,"editbook.html",{"book_obj":book_obj,"pub_list": pub_list, "author_list": author_list})
    
    # def query(request):
    #     ret = Book.objects.filter(publish="老男孩出版社", price__gt=200)
    #     ret = Book.objects.filter(title__startswith="py")
    #     return HttpResponse("ok")
    
    
    #查看出版社
    def publisher(request):
        publisher_list = models.Publisher.objects.all()
        return render(request, "publisher.html",{"publisher_list":publisher_list} )
    
    def pub_is_valid(xname):
        """验证出版社信息"""
        if models.Publisher.objects.filter(pname=xname).count() != 0:
            return {"status": "该出版社已存在!"}
    #增加出版社
    def addpublisher(request):
        if request.method == 'POST':
            pname = request.POST.get("pname")
            ret = pub_is_valid(pname)
            # print(ret)
            if ret:
                ret["pname"] = pname
                return render(request,"addpublisher.html",ret)
            else:
                models.Publisher.objects.create(pname=pname)
                return redirect("/publisher/")
        return render(request, "addpublisher.html")
    
    #编辑出版社
    def editpublisher(request):
        pid = request.GET.get("pid")
        pub_obj = models.Publisher.objects.filter(id=pid).first()
        if request.method == "POST":
            pname = request.POST.get("pname")
            ret = pub_is_valid(pname)
            if ret:
                pub_obj.pname = pname
                ret["pub_obj"] = pub_obj
                return render(request,"editpublisher.html",ret)
            else:
                models.Publisher.objects.filter(id=pid).update(pname=pname)
                return redirect("/publisher/")
        return render(request, "editpublisher.html", {"pub_obj":pub_obj})
    
    #删除出版社
    def delpublisher(request):
        pid = request.GET.get("pid")
        publisher_obj = models.Publisher.objects.filter(id=pid).first()
        publisher_obj.delete()
        return redirect("/publisher/")
    
    #出版社关联书籍
    def publisher_book(request):
        pid = request.GET.get("pid")
        pub_obj = models.Publisher.objects.filter(id=pid).first()
        pname = pub_obj.pname
        book_list = pub_obj.book_set.all()
        return render(request, "publisher_book.html")
    #查看作者
    def author(request):
        author_list = models.Author.objects.all()
        return render(request, "author.html", {"author_list":author_list})
    
    def author_is_valid(xname):
        """验证作者信息"""
        if models.Author.objects.filter(aname=xname).count() != 0:
            return {"status": "该作者已存在!"}
    
    def addauthor(request):
        if request.method == 'POST':
            aname = request.POST.get("aname")
            ret = author_is_valid(aname)
            if ret:
                ret["aname"] = aname
                return render(request,"addauthor.html",ret)
            else:
                models.Author.objects.create(aname=aname)
                return redirect("/author/")
    
        return render(request, "addauthor.html")
    
    def delauthor(request):
        aid = request.GET.get("aid")
        author_obj = models.Author.objects.filter(id=aid).first()
        author_obj.delete()
        return redirect("/author/")
    
    # 编辑作者
    def editauthor(request):
        aid = request.GET.get("aid")
        author_obj = models.Author.objects.filter(id=aid).first()
        if request.method == "POST":
            aname = request.POST.get("aname")
            ret = author_is_valid(aname)
            if ret:
                author_obj.aname = aname
                ret["author_obj"] = author_obj
                return render(request,"editauthor.html",ret)
            else:
                models.Author.objects.filter(id=aid).update(aname=aname)
                return redirect("/author/")
        return render(request,"editauthor.html",{"author_obj":author_obj})
    
    #作者关联书籍;多对多
    def author_book(request):
        aid = request.GET.get("aid")
        author_obj = models.Author.objects.filter(id=aid).first()
        aname = author_obj.aname
        book_list = author_obj.book_set.all()
        return render(request, "author_book.html")

    settings.py

     

    base.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
     7 
     8 </head>
     9 <body>
    10 <div class="container">
    11     <h2>图书管理系统</h2>
    12     <br>
    13 </div>
    14 
    15 <div class="container">
    16     <ul class="nav nav-tabs" id="myTab">
    17         <li role="presentation" class="active"><a href="/index/">主页</a></li>
    18         <li role="presentation"><a href="/book/">图书</a></li>
    19         <li role="presentation"><a href="/author/">作者</a></li>
    20         <li role="presentation"><a href="/publisher/">出版社</a></li>
    21     </ul>
    22 </div>
    23 <br>
    24 
    25 {% block content %}
    26 {% endblock %}
    27 
    28 
    29 </body>
    30 <script src="/static/js/jquery-3.3.1.js"></script>
    31 <script src="/static/bs/js/bootstrap.js"></script>
    32 
    33 <script>
    34     {% block script %}
    35     {% endblock %}
    36 </script>
    37 </html>
    View Code

    addauthor.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <div class="col-md-4">
     6             <h4>添加作者信息</h4>
     7             <form action="/addauthor/" method="post">
     8                 {% csrf_token %}
     9                 <div class="form-group">
    10                     <label for="author">作者姓名</label>
    11                     <input type="text" class="form-control" id="author" name="aname"
    12                            placeholder="请输入要添加的作者姓名" value="{{ aname }}" style="margin-bottom: 5px;">
    13                     <span class="label label-danger">{{ status }}</span>
    14                 </div>
    15                 <div class="form-group">
    16                     <button type="submit" class="btn btn-primary">提交</button>
    17                 </div>
    18             </form>
    19         </div>
    20     </div>
    21 
    22 {% endblock %}
    View Code

    addbook.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <div class="col-md-4">
     6             <h4>添加图书信息</h4>
     7             <form action="/addbook/" method="post">
     8                 {% csrf_token %}
     9                 <div class="form-group">
    10                     <label for="book">书名</label>
    11                     <input type="text" class="form-control" id="book" name="title"
    12                            placeholder="请输入要添加的书籍名称">
    13                 </div>
    14                 <div class="form-group">
    15                     <b style="margin-bottom: 5px;display: inline-block">请选择出版社</b>
    16                     <select class="form-control" name="pid">
    17                         {% for pub in pub_list %}
    18                             <option value={{ pub.id }}>{{ pub.pname }}</option>
    19                         {% endfor %}
    20                     </select>
    21                 </div>
    22                 <div class="form-group">
    23                     <label for="date">出版日期</label>
    24                     <input type="date" class="form-control" id="date" name="pub_date">
    25                 </div>
    26                 <div class="form-group">
    27                     <b style="margin-bottom: 5px;display: inline-block">请选择作者(可多选)</b>
    28                     <select class="form-control" multiple name="aids">
    29                         {% for author in author_list %}
    30                             <option value={{ author.id }}>{{ author.aname }}</option>
    31                         {% endfor %}
    32                     </select>
    33                 </div>
    34                 <div class="form-group">
    35                     <button type="submit" class="btn btn-primary">提交</button>
    36                 </div>
    37             </form>
    38         </div>
    39     </div>
    40 
    41 {% endblock %}
    View Code

    addpublisher.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <div class="col-md-4">
     6             <h4>添加出版社信息</h4>
     7             <form action="/addpublisher/" method="post">
     8                 {% csrf_token %}
     9                 <div class="form-group">
    10                     <label for="publisher">出版社名称</label>
    11                     <input type="text" class="form-control" id="publisher" name="pname"
    12                            placeholder="请输入要添加的出版社名称" value="{{ pname }}" style="margin-bottom: 5px;">
    13                     <span class="label label-danger">{{ status }}</span>
    14                 </div>
    15                 <div class="form-group">
    16                     <button type="submit" class="btn btn-primary">提交</button>
    17                 </div>
    18             </form>
    19         </div>
    20     </div>
    21 
    22 {% endblock %}
    View Code

    author.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container" style="padding-bottom: 5px;">
     5         <a href="/addauthor/" class="btn btn-info glyphicon glyphicon-plus">添加作者</a>
     6     </div>
     7     <div class="container">
     8         <table class="table table-striped table-hover">
     9             <thead>
    10             <tr>
    11                 <th>序号</th>
    12                 <th hidden>作者ID</th>
    13                 <th>作者</th>
    14                 <th>操作</th>
    15             </tr>
    16             </thead>
    17             <tbody>
    18             {% for author in author_list %}
    19                 <tr>
    20                     <td>{{ forloop.counter }}</td>
    21                     <td hidden>{{ author.id }}</td>
    22                     <td><a href="/author_book?aid={{ author.id }}">{{ author.aname }}</a></td>
    23                     <td>
    24                         <a class="glyphicon glyphicon-pencil" style="padding-right: 10px" href="/editauthor?aid={{ author.id }}"></a>
    25                         <a class="glyphicon glyphicon-remove" href="/delauthor?aid={{ author.id }}"></a>
    26                     </td>
    27 
    28                 </tr>
    29             {% endfor %}
    30             </tbody>
    31         </table>
    32 
    33     </div>
    34 
    35 {% endblock %}
    36 
    37 {% block script %}
    38     $(function () {
    39         $("li[class=active]").parent().children().eq(2).addClass("active").siblings().removeClass("active");
    40     })
    41 {% endblock %}
    View Code

    author_book.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <table class="table table-striped table-hover">
     6             <thead>
     7             <tr>
     8                 <th>序号</th>
     9                 <th>出版书籍</th>
    10                 <th>作者</th>
    11             </tr>
    12             </thead>
    13             <tbody>
    14             {% for book in book_list %}
    15                 <tr>
    16                     <td>{{ forloop.counter }}</td>
    17                     <td>{{ book.title }}</td>
    18                     <td>{{ aname }}</td>
    19                 </tr>
    20             {% endfor %}
    21             </tbody>
    22         </table>
    23 
    24     </div>
    25 
    26 {% endblock %}
    View Code

    book.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container" style="padding-bottom: 5px;">
     5         <a href="/addbook/" class="btn btn-info glyphicon glyphicon-plus">添加图书</a>
     6     </div>
     7     <div class="container">
     8         <table class="table table-striped table-hover">
     9             <thead>
    10             <tr>
    11                 <th>序号</th>
    12                 <th hidden>图书ID</th>
    13                 <th>书名</th>
    14                 <th>作者</th>
    15                 <th>出版日期</th>
    16                 <th>出版社</th>
    17                 <th>操作</th>
    18             </tr>
    19             </thead>
    20             <tbody>
    21             {% for book in book_list %}
    22                 <tr>
    23                     <td>{{ forloop.counter }}</td>
    24                     <td hidden>{{ book.id }}</td>
    25                     <td>{{ book.title }}</td>
    26                     <td>
    27                         {% for author in book.author.all %}
    28                             {{ author.aname }}&nbsp;&nbsp;&nbsp;
    29                         {% endfor %}
    30                     </td>
    31                     <td>{{ book.publish_date|date:"Y-m-d" }}</td>  {# filter格式化日期 #}
    32                     <td>{{ book.publisher.pname }}</td>
    33                     <td>
    34                         <a class="glyphicon glyphicon-pencil" style="padding-right: 10px" href="/editbook?bid={{ book.id }}"></a>
    35                         <a class="glyphicon glyphicon-remove" href="/delbook?bid={{ book.id }}"></a>
    36                     </td>
    37 
    38                 </tr>
    39             {% endfor %}
    40             </tbody>
    41         </table>
    42 
    43     </div>
    44 
    45 {% endblock %}
    46 
    47 {% block script %}
    48     $(function () {
    49         $("li[class=active]").parent().children().eq(1).addClass("active").siblings().removeClass("active");
    50     })
    51 {% endblock %}
    View Code

    editauthor.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <div class="col-md-4">
     6             <h4>编辑作者信息</h4>
     7              <form action="/editauthor/?aid={{ author_obj.id }}" method="post">
     8                 {% csrf_token %}
     9                 <div class="form-group">
    10                     <label for="author">作者姓名</label>
    11                     <input type="text" class="form-control" style="margin-bottom: 5px;" id="author" name="aname" value="{{ author_obj.aname }}">
    12                     <span class="label label-danger">{{ status }}</span>
    13                 </div>
    14                 <div class="form-group">
    15                     <button type="submit" class="btn btn-primary">提交</button>
    16                 </div>
    17             </form>
    18         </div>
    19 
    20     </div>
    21     
    22 {% endblock %}
    View Code

    editbook.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <div class="col-md-4">
     6             <h4>编辑图书信息</h4>
     7             <form action="/editbook/?bid={{ book_obj.id }}" method="post">
     8                 {% csrf_token %}
     9                 <div class="form-group">
    10                     <label for="book">书名</label>
    11                     <input type="text" class="form-control" id="book" name="title"
    12                            value="{{ book_obj.title }}">
    13                 </div>
    14 
    15                 <div class="form-group">
    16                     <b style="margin-bottom: 5px;display: inline-block">请选择出版社</b>
    17                     <select class="form-control" name="pid">
    18                         {% for pub in pub_list %}
    19                             {% if pub.id == book_obj.publisher.id %}
    20                                 <option value="{{ pub.id }}" selected>{{ pub.pname }}</option>
    21                             {% else %}
    22                                 <option value="{{ pub.id }}">{{ pub.pname }}</option>
    23                             {% endif %}
    24                         {% endfor %}
    25                     </select>
    26                 </div>
    27 
    28                 <div class="form-group">
    29                     <label for="date">出版日期</label>
    30                     <input type="date" class="form-control" id="date" name="pub_date"
    31                            value="{{ book_obj.publish_date|date:"Y-m-d" }}">
    32                 </div>
    33 
    34                 <div class="form-group">
    35                     <b style="margin-bottom: 5px;display: inline-block">请选择作者(可多选)</b>
    36                     <select class="form-control" multiple name="aids">
    37                         {% for author in author_list %}
    38                             {% if author in book_obj.author.all %}
    39                                 <option value="{{ author.id }}" selected>{{ author.aname }}</option>
    40                             {% else %}
    41                                 <option value="{{ author.id }}">{{ author.aname }}</option>
    42                             {% endif %}
    43                         {% endfor %}
    44                     </select>
    45                 </div>
    46 
    47                 <div class="form-group">
    48                     <button type="submit" class="btn btn-primary">提交</button>
    49                 </div>
    50 
    51             </form>
    52         </div>
    53     </div>
    54 
    55 {% endblock %}
    View Code

    editpublisher.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <div class="col-md-4">
     6             <h4>编辑出版社信息</h4>
     7             <form action="/editpublisher/?pid={{ pub_obj.id }}" method="post">
     8                 {% csrf_token %}
     9                 <div class="form-group">
    10                     <label for="publisher">出版社名称</label>
    11                     <input type="text" class="form-control" style="margin-bottom: 5px;" id="publisher" name="pname"
    12                            value="{{ pub_obj.pname }}">
    13                     <span class="label label-danger">{{ status }}</span>
    14                 </div>
    15                 <div class="form-group">
    16                     <button type="submit" class="btn btn-primary">提交</button>
    17                 </div>
    18             </form>
    19         </div>
    20 
    21     </div>
    22 
    23 {% endblock %}
    View Code

    publisher.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container" style="padding-bottom: 5px;">
     5         <a href="/addpublisher/" class="btn btn-info glyphicon glyphicon-plus">添加出版社</a>
     6     </div>
     7 
     8     <div class="container">
     9         <table class="table table-striped table-hover">
    10             <thead>
    11             <tr>
    12                 <th>序号</th>
    13                 <th hidden>出版社ID</th>
    14                 <th>出版社</th>
    15                 <th>操作</th>
    16             </tr>
    17             </thead>
    18             <tbody>
    19             {% for pub in publisher_list %}
    20                 <tr>
    21                     <td>{{ forloop.counter }}</td>
    22                     <td hidden>{{ pub.id }}</td>
    23                     <td><a href="/publisher_book?pid={{ pub.id }}">{{ pub.pname }}</a></td>
    24                     <td>
    25                         <a class="glyphicon glyphicon-pencil" style="padding-right: 10px" href="/editpublisher?pid={{ pub.id }}"></a>
    26                         <a class="glyphicon glyphicon-remove" href="/delpublisher?pid={{ pub.id }}"></a>
    27                     </td>
    28 
    29                 </tr>
    30             {% endfor %}
    31             </tbody>
    32         </table>
    33     </div>
    34 
    35 {% endblock %}
    36 
    37 {% block script %}
    38     $(function () {
    39         $("li[class=active]").parent().children().eq(3).addClass("active").siblings().removeClass("active");
    40     })
    41 {% endblock %}
    View Code

    publisher_book.html

     1 {% extends "base.html" %}
     2 {% block content %}
     3 
     4     <div class="container">
     5         <table class="table table-striped table-hover">
     6             <thead>
     7             <tr>
     8                 <th>序号</th>
     9                 <th>出版书籍</th>
    10                 <th>出版社</th>
    11             </tr>
    12             </thead>
    13             <tbody>
    14             {% for book in book_list %}
    15                 <tr>
    16                     <td>{{ forloop.counter }}</td>
    17                     <td>{{ book.title }}</td>
    18                     <td>{{ pname }}</td>
    19 
    20                 </tr>
    21             {% endfor %}
    22             </tbody>
    23         </table>
    24 
    25     </div>
    26 
    27 {% endblock %}
    View Code

    index.html

    1 {% extends "base.html" %}
    2 {% block content %}
    3 
    4 <div class="container"><h2>Welcome to the library management system</h2></div>
    5 
    6 {% endblock %}
    View Code

     
  • 相关阅读:
    多个漂亮的按钮样式和图片集合
    纯CSS3实现3D跳动小球
    visual studio 查找/替换对话框
    CSS实现弹出导航菜单
    javascript使浏览器关闭前弹出确认
    使用CSS3制作立体效果的导航菜单
    多个精美的导航样式web2.0源码
    jQuery实现侧边导航栏效果
    jQ函数after、append、appendTo的区别
    ASP.NET使用jQuery AJAX实现MD5加密实例
  • 原文地址:https://www.cnblogs.com/shengyang17/p/9131636.html
Copyright © 2020-2023  润新知