创建一个简易的可以增删改查book的书籍管理system

urls.py

from django.contrib import admin
from django.urls import re_path,path
from book import views

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('books/add/$', views.add_book),
    re_path('books/$', views.books),
    re_path('layout/$', views.layout),
    re_path('motai/$', views.motai),
    re_path('books/(\d+)/change/$', views.change_book),
    re_path('books/(\d+)/delete/$', views.delete_book),
]

settings.py

ROOT_URLCONF = 'bookms_02.urls'

views.py

from django.shortcuts import render,HttpResponse,redirect

# Create your views here.

from .models import Publish,Author,Book

def add_book(request):

    if request.method=="POST":
        title=request.POST.get("title")
        price=request.POST.get("price")
        pub_date=request.POST.get("pub_date")
        publish_id=request.POST.get("publish_id")
        authors_id_list=request.POST.getlist("authors_id_list") # checkbox,select

        book_obj=Book.objects.create(title=title,price=price,publishDate=pub_date,publish_id=publish_id)
        print(authors_id_list) # ['2', '3']

        book_obj.authors.add(*authors_id_list)
        return redirect("/books/")


    publish_list=Publish.objects.all()
    author_list=Author.objects.all()

    return render(request,"addbook.html",{"author_list":author_list,"publish_list":publish_list})

def books(request):

    book_list=Book.objects.all()
    return render(request,"books.html",{"book_list":book_list})

def change_book(request,edit_book_id):
    edit_book_obj=Book.objects.filter(pk=edit_book_id).first()

    if  request.method=="POST":
        title=request.POST.get("title")
        price=request.POST.get("price")
        pub_date=request.POST.get("pub_date")
        publish_id=request.POST.get("publish_id")
        authors_id_list=request.POST.getlist("authors_id_list") # checkbox,select

        Book.objects.filter(pk=edit_book_id).update(title=title,price=price,
                                                    publishDate=pub_date,publish_id=publish_id)
        # edit_book_obj.authors.clear()
        # edit_book_obj.authors.add(*authors_id_list)

        edit_book_obj.authors.set(authors_id_list)

        return redirect("/books/")

    publish_list=Publish.objects.all()
    author_list=Author.objects.all()

    return render(request,"editbook.html",{"edit_book_obj":edit_book_obj,"publish_list":publish_list,"author_list":author_list})

def delete_book(request,delete_book_id):


    Book.objects.filter(pk=delete_book_id).delete()

    return redirect("/books/")

def layout(request):

    return render(request,'layout.html')

def motai(request):
    return render(request,'motai.html')

models.py

from django.db import models

# Create your models here.


class Author(models.Model):
    nid = models.AutoField(primary_key=True)
    name=models.CharField( max_length=32)
    age=models.IntegerField()

class Publish(models.Model):
    nid = models.AutoField(primary_key=True)
    name=models.CharField( max_length=32)
    city=models.CharField( max_length=32)
    email=models.EmailField()

class Book(models.Model):

    nid = models.AutoField(primary_key=True)
    title = models.CharField( max_length=32)
    publishDate=models.DateField()
    price=models.DecimalField(max_digits=5,decimal_places=2) # 999.99

    # 与Publish建立一对多的关系,外键字段建立在多的一方
    publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE)
    # 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表
    authors=models.ManyToManyField(to='Author',)

motai.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .hide{
            display: none;
        }
        .shadow{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color:black;
            opacity: 0.4;
            z-index: 999;
        }
        .modal{
            z-index: 1000;
            position: fixed;
            left: 50%;
            top: 50%;
            height: 300px;
            width: 400px;
            background-color: white;
            margin-left: -200px;
            margin-top: -150px;
        }
    </style>
</head>
<body>
    <a onclick="showModal();" href="">对话框添加</a>
    <div id="shadow" class="shadow hide"></div>
    <div id="modal" class="modal hide">
        <form action="" method="post">
            <p>
                <input type="text" name="title">
            </p>
            <input type="submit" value="提交">
        </form>
    </div>
<script>
    function showModal() {
        document.getElementById('shadow').classList.remove('hide');
        document.getElementById('modal').classList.remove('hide');

    }



</script>

</body>
</html>
View Code

layout.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.css">
    <link rel="stylesheet" href="/static/css/commons.css">
    {% block css %}{% endblock %}
</head>
<body>

<div class="pg-header">
    <div class="logo left">老女孩后台管理</div>
    <div class="avator right">
        <img src="/static/images/1.jpg" alt="">
        <div class="user-info">
            <a href="">个人资料</a>
            <a href="">注销</a>
        </div>
    </div>
    <div class="rmenus right">
        <a href=""><i class="fa fa-commenting-o" aria-hidden="true"></i> 消息</a>
        <a href=""><i class="fa fa-envelope-o" aria-hidden="true"></i> 邮件</a>

    </div>


</div>
<div class="pg-body">
    <div class="menus">
        <a href=""><i class="fa fa-bug" aria-hidden="true"></i> 班级管理</a>
        <a href=""><i class="fa fa-blind" aria-hidden="true"></i> 学生管理</a>
        <a href=""><i class="fa fa-flag" aria-hidden="true"></i> 教师管理</a>

    </div>
    <div class="content">
        <ol class="breadcrumb">
            <li><a href="#">首页</a></li>
            <li><a href="#">班级管理</a></li>
            <li class="active">添加班级</li>
        </ol>
        <div>
        <div class="row">
            <div class="col-md-offset-2 col-md-8">
                {% block content %}
                {% endblock %}

            </div>

        </div>

    </div>

        <nav aria-label="Page navigation">
              <ul class="pagination">
    <li>
      <a href="#" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li>
      <a href="#" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>
    </div>
</div>

{% block js %}

{% endblock %}
</body>
</html>
View Code

addbook.html

{% extends 'layout.html' %}


{% block content %}
   <h3>增加书籍</h3>
    <form action="" method="post">
                 {% csrf_token %}
                 <div class="form-group">
                     <label for="">名称</label>
                     <input type="text" name="title" class="form-control" value="">
                 </div>

                <div class="form-group">
                     <label for="">价格</label>
                     <input type="text" name="price" class="form-control">
                 </div>

                <div class="form-group">
                     <label for="">出版日期</label>
                     <input type="date" name="pub_date" class="form-control">
                 </div>

                <div class="form-group">
                     <label for="">出版社</label>
                    <select name="publish_id" id="" class="form-control">
                           {% for publish in publish_list %}
                           <option value="{{ publish.pk }}">{{ publish.name }}</option>
                           {% endfor %}
                    </select>
                 </div>

                <div class="form-group">
                     <label for="">作者</label>
                     <select type="text" name="authors_id_list" multiple class="form-control">
                         {% for author in author_list %}
                           <option value="{{ author.pk }}">{{ author.name }}</option>
                           {% endfor %}

                     </select>
                 </div>
                <input type="submit" class="btn btn-default">

            </form>
{% endblock %}
View Code

books.html

{% extends 'layout.html' %}

{% block css %}

{% endblock %}


{% block content %}
       <a href="/books/add/" class="fa fa-handshake-o">添加书籍</a>
        <table class="table table-bordered table-hover table-striped">

                 <thead>
                    <tr>
                         <th>编号</th>
                         <th>书籍名称</th>
                         <th>价格</th>
                         <th>出版日期</th>
                         <th>出版社</th>
                         <th>作者</th>
                         <th>操作</th>

                    </tr>
                 </thead>
                 <tbody>
                      {% for book in book_list %}
                      <tr>
                           <td>{{ forloop.counter }}</td>
                           <td>{{ book.title }}</td>
                           <td>{{ book.price }}</td>
                           <td>{{ book.publishDate|date:"Y-m-d" }}</td>
                           <td>
                               {{ book.publish.name }}
                           </td>
                           <td>
                               {% for author in book.authors.all %}
                                  {% if forloop.last %}
                                   <span>{{ author.name }}</span>
                                  {% else %}
                                   <span>{{ author.name }}</span>,
                                  {% endif %}

                               {% endfor %}

                           </td>

                           <td>
                               <a href="/books/{{ book.pk }}/change/" class="glyphicon glyphicon-pencil">编辑</a>
                               <a href="/books/{{ book.pk }}/delete/" class="glyphicon glyphicon-trash">删除</a>
                           </td>

                      </tr>
                      {% endfor %}

                 </tbody>
             </table>
{% endblock %}

{% block js %}

{% endblock %}



<!--<!DOCTYPE html>-->
<!--<html lang="en">-->
<!--<head>-->
    <!--<meta charset="UTF-8">-->
    <!--<title>Title</title>-->
    <!--<link rel="stylesheet" href="/static/bs/css/bootstrap.css">-->
    <!--<link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.css">-->

<!--</head>-->
<!--<body>-->

<!--<h3>查看书籍</h3>-->


<!--<div class="container">-->
    <!--<div class="row">-->
        <!--<div class="col-md-8 col-md-offset-2">-->

            <!--<a href="/books/add/" class="fa fa-handshake-o"></a>-->
             <!--<table class="table table-bordered table-hover table-striped">-->

                 <!--<thead>-->
                    <!--<tr>-->
                         <!--<th>编号</th>-->
                         <!--<th>书籍名称</th>-->
                         <!--<th>价格</th>-->
                         <!--<th>出版日期</th>-->
                         <!--<th>出版社</th>-->
                         <!--<th>作者</th>-->
                         <!--<th>操作</th>-->

                    <!--</tr>-->
                 <!--</thead>-->
                 <!--<tbody>-->
                      <!--{% for book in book_list %}-->
                      <!--<tr>-->
                           <!--<td>{{ forloop.counter }}</td>-->
                           <!--<td>{{ book.title }}</td>-->
                           <!--<td>{{ book.price }}</td>-->
                           <!--<td>{{ book.publishDate|date:"Y-m-d" }}</td>-->
                           <!--<td>-->
                               <!--{{ book.publish.name }}-->
                           <!--</td>-->
                           <!--<td>-->
                               <!--{% for author in book.authors.all %}-->
                                  <!--{% if forloop.last %}-->
                                   <!--<span>{{ author.name }}</span>-->
                                  <!--{% else %}-->
                                   <!--<span>{{ author.name }}</span>,-->
                                  <!--{% endif %}-->

                               <!--{% endfor %}-->

                           <!--</td>-->

                           <!--<td>-->
                               <!--<a href="/books/{{ book.pk }}/change/" class="glyphicon glyphicon-pencil"></a>-->
                               <!--<a href="/books/{{ book.pk }}/delete/" class="glyphicon glyphicon-trash"></a>-->
                           <!--</td>-->

                      <!--</tr>-->
                      <!--{% endfor %}-->

                 <!--</tbody>-->
             <!--</table>-->
        <!--</div>-->
    <!--</div>-->
<!--</div>-->


<!--&lt;!&ndash; Button trigger modal &ndash;&gt;-->
<!--<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">-->
  <!--Launch demo modal-->
<!--</button>-->
<!--<div class="modal fade" tabindex="-1" role="dialog">-->
  <!--<div class="modal-dialog" role="document">-->
    <!--<div class="modal-content">-->
      <!--<div class="modal-header">-->
        <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>-->
        <!--<h4 class="modal-title">Modal title</h4>-->
      <!--</div>-->
      <!--<div class="modal-body">-->
        <!--<p>One fine body&hellip;</p>-->
      <!--</div>-->
      <!--<div class="modal-footer">-->
        <!--<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>-->
        <!--<button type="button" class="btn btn-primary">Save changes</button>-->
      <!--</div>-->
    <!--</div>&lt;!&ndash; /.modal-content &ndash;&gt;-->
  <!--</div>&lt;!&ndash; /.modal-dialog &ndash;&gt;-->
<!--</div>&lt;!&ndash; /.modal &ndash;&gt;-->

<!--</body>-->
<!--</html>-->
View Code

editbook.html

{% extends 'layout.html' %}


{% block content %}
    <h3>编辑书籍</h3>
    <form action="" method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="">名称</label>
                        <input type="text" name="title" class="form-control" value="{{ edit_book_obj.title }}">
                    </div>

                    <div class="form-group">
                        <label for="">价格</label>
                        <input type="text" name="price" class="form-control" value="{{ edit_book_obj.price }}">
                    </div>

                    <div class="form-group">
                        <label for="">出版日期</label>
                        <input type="date" name="pub_date" class="form-control"
                               value="{{ edit_book_obj.publishDate|date:'Y-m-d' }}">
                    </div>

                    <div class="form-group">
                        <label for="">出版社</label>
                        <select name="publish_id" id="" class="form-control">
                            {% for publish in publish_list %}
                                {% if edit_book_obj.publish == publish %}
                                    <option selected value="{{ publish.pk }}">{{ publish.name }}</option>
                                {% else %}
                                    <option value="{{ publish.pk }}">{{ publish.name }}</option>
                                {% endif %}
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="">作者</label>
                        <select type="text" name="authors_id_list" multiple class="form-control">
                            {% for author in author_list %}

                            {% if author in edit_book_obj.authors.all %}
                                <option selected value="{{ author.pk }}">{{ author.name }}</option>
                            {% else %}
                                 <option value="{{ author.pk }}">{{ author.name }}</option>
                            {% endif %}

                            {% endfor %}
                        </select>
                    </div>
                    <input type="submit" class="btn btn-default">

                </form>

{% endblock %}
View Code