图书管理系统(一)

表设计

model层

先考虑普通字段 再考虑外键字段

from django.db import models


# Create your models here.
class Book(models.Model):
    name = models.CharField(max_length=32, verbose_name='书名')
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='价格')  # decimal是精度最高的浮点型字段:decimal(10,2) 10表示支持10位数字,2表示小数点后有两位
    publish_time = models.DateField(auto_now_add=True, verbose_name='出版日期')
    stock = models.IntegerField(verbose_name='库存数', default=1000)
    sale = models.IntegerField(verbose_name='卖出数', null=True)

    # 一对多
    publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)  # 级联删除
    # 多对多
    authors = models.ManyToManyField(to='Author')  # 通过虚拟字段创建第三张表


class Publish(models.Model):
    name = models.CharField(max_length=32, verbose_name='名称')
    address = models.CharField(max_length=64, verbose_name='地址')


class Author(models.Model):
    name = models.CharField(max_length=32, verbose_name='姓名')
    age = models.IntegerField(verbose_name='年龄')
    # 一对一
    author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)  # 一对一、一对多都需要添加on_delete


class AuthorDetail(models.Model):
    phone = models.BigIntegerField(verbose_name='手机号')  # 手机号是13位 普通的int字段装不下
    address = models.CharField(max_length=64, verbose_name='家庭住址')

数据库迁移失败问题

'''
一个数据库尽量只对应一个django项目
不要出现多个django项目使用一个数据库 极其容易报错。
day1   -->    db1
day2   -->    db1   不能这样做
'''

而要每次创建一个新的数据库。

图书管理系统

创建首页

首先首页要作为一个母模板给子板继承,所以至少要对Css\js\html进行分块。至少分3块,供子板可以在继承之后进行修改。
母版的导航条、侧板栏均不动,只有右侧的面板,随着不同链接而变换成不同内容。
image

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

    {% load static %}

    {% block link_css %}
        <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    {% endblock %}

    {% block link_js %}
        <script src="{% static 'js/jquery3.6.1.js' %}"></script>
        <script src="{% static 'js/bootstrap.min.js' %}"></script>
    {% endblock %}

    {% block style_css %}
        <style></style>
    {% endblock %}

</head>
<body>
<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-2">
                <div class="list-group">
                  <a href="{% url 'home_view' %}" class="list-group-item active">
                    首页展示
                  </a>
                  <a href="{% url 'book_list_view' '1' %}" class="list-group-item">图书列表</a>
                  <a href="#" class="list-group-item">出版社列表</a>
                  <a href="#" class="list-group-item">作者列表</a>
                  <a href="#" class="list-group-item">更多详情</a>
                </div>
                <div class="list-group">
                  <a href="#" class="list-group-item active">
                    首页展示
                  </a>
                  <a href="#" class="list-group-item">图书列表</a>
                  <a href="#" class="list-group-item">出版社列表</a>
                  <a href="#" class="list-group-item">作者列表</a>
                  <a href="#" class="list-group-item">更多详情</a>
                </div>








            </div>
            <div class="col-md-10">
                <div class="panel panel-primary">
                  <div class="panel-heading"></div>
                  <div class="panel-body">

                      {% block panel-body %}

                      <div class="page-header">
                          <h1>图书管理系统 <small>更多精彩等你来!</small></h1>
                      </div>
                      <div class="jumbotron">
                              <h1>全球最大的线上网站</h1>
                              <p>友情链接</p>
                              <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
                       </div>
                      <div class="col-sm-6 col-md-4">
                      <div class="thumbnail">
                      <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fblog.sciencenet.cn%2Fdata%2Fattachment%2Falbum%2F201406%2F14%2F2010274s7c9u2z1zs01cos.jpg&refer=http%3A%2F%2Fblog.sciencenet.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1673854705&t=c8736bbec8cd4a69f6ea79a253270110" alt="...">
                      <div class="caption">
                        <h3>Thumbnail label</h3>
                        <p>...</p>
                        <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                      </div>
                    </div>
                  </div>
                      <div class="col-sm-6 col-md-4">
                        <div class="thumbnail">
                          <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2Fv2-8f5e6ff87fb61755e9ab352652b4bc4e_1200x500.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1673855229&t=5e9079863fcb302eadd679eb2671a5ef" alt="...">
                          <div class="caption">
                            <h3>Thumbnail label</h3>
                            <p>...</p>
                            <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                          </div>
                        </div>
                     </div>
                      <div class="col-sm-6 col-md-4">
                        <div class="thumbnail">
                          <img src="https://img2.baidu.com/it/u=2536325439,2761586481&fm=253&fmt=auto&app=138&f=JPEG?w=483&h=569" alt="...">
                          <div class="caption">
                            <h3>Thumbnail label</h3>
                            <p>...</p>
                            <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                          </div>
                        </div>
                     </div>

                      {% endblock %}

                  </div>
                </div>





            </div>
        </div>
    </div>

{% block local_js %}
    <script></script>
{% endblock %}

</body>
</html>

图书展示页

图书展示页需要获取所有书籍对象,经过模板渲染,然后展示数据。
本页使用:
1.forloop.counter:从列表中取出书籍对象时,获取当前书籍是第几次循环取出的。
2.反向解析
3.针对日期展示需要格式化:使用过滤器
book_obj.publish_time|date:'Y-m-d'
4.展示作者:使用for循环将作者列表中的作者对象拿出来。
5.分页展示
image

{% extends 'home_page.html' %}

{% block panel-body %}
    <h2 class="text-center">图书展示页</h2>
    <a href="{% url 'book_add_view' %}" class="btn btn-success">添加书籍</a>
    <table class="table table-striped table-hover">
        <thead>
            <tr>
                <th>编号</th>
                <th>书名</th>
                <th>价格</th>
                <th>日期</th>
                <th>出版社</th>
                <th>库存</th>
                <th>卖出</th>
                <th>作者</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
        {% for book_obj in book_queryset|slice:page_all_book  %}
            <tr>
                <td> {{ forloop.counter }} </td>
                <td> {{ book_obj.name }} </td>
                <td> {{ book_obj.price }} </td>
                <td> {{ book_obj.publish_time|date:'Y-m-d' }} </td>
                <td> {{ book_obj.publish.name }} </td>  <!-- 注意这里 -->
                <td> {{ book_obj.stock }} </td>
                <td> {{ book_obj.sale }} </td>
                <td>
{#                    {% for author_obj in book_obj.authors.all %}#}
{#                        <span>{{ author_obj.name }}</span>#}
{#                    {% endfor %}#}
                    {% for author_obj in book_obj.authors.all %}
                        {% if forloop.last %}
                            <span>{{ author_obj.name }}</span>
                        {% else %}
                            <span>{{ author_obj.name }},</span>
                        {% endif %}
                    {% endfor %}

                </td>
                <td>
                    <a href="{% url 'book_edit_view' book_obj.pk %}" class="btn btn-primary btn-xs">编辑</a>
                    <a href="{% url 'book_delete_view' book_obj.pk %}" class="btn btn-danger btn-xs btuDel">删除</a>   <!-- 需要对每一个按钮都添加事件 所以应该设置按钮的类 -->
                </td>

            </tr>


        {% endfor %}




        </tbody>
    </table>
    <nav aria-label="Page navigation" class="text-center">
      <ul class="pagination">
        <li>
          <a href="#" aria-label="Previous">
            <span aria-hidden="true">&laquo;</span>
          </a>
        </li>
         {% for page in page_list %}
              <li><a href="{% url 'book_list_view' page %}">{{ page }}</a></li>
         {% endfor %}

        <li>
          <a href="#" aria-label="Next">
            <span aria-hidden="true">&raquo;</span>
          </a>
        </li>
      </ul>
    </nav>


{% endblock %}


{% block local_js %}
    <script>
        $('.btuDel').click(function (){
            let res = confirm('确定要删除吗?')
            if (res){
            } else {
                return false;
            }
        })
    </script>
{% endblock %}

图书添加页

image

展示系统里面已经有的出版社信息,让用户选择。
如果没有出版社,应该去添加出版社。
展示系统里面已经有的作者信息,让用户选择。

{% extends 'home_page.html' %}

<!-- 这里这个添加页的页面,需要两个数据:出版社 作者 -->

{% block panel-body %}
    <div class="col-md-8 col-md-offset-2" >
        <h2 class="text-center">图书添加页</h2>
        <form action="" method="post">
        <p>书名:
            <input type="text" name="name" class="form-control">
        </p>
        <p>价格:
            <input type="text" name="price" class="form-control">
        </p>
        <p>出版时间:
            <input type="date" name="publish_time" class="form-control">
        </p>
        <p>出版社:
            <select name="publish_id" class="form-control">
                {% for publish_obj in publish_queryset %}
                    <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
                {% endfor %}
            </select>
        </p>
        <p>作者:
            <select name="author_id_list" class="form-control" multiple>
                {% for author_obj in author_queryset %}
                    <option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
                {% endfor %}
            </select>
        </p>
        <input type="submit" value="添加书籍" class="btn btn-success">
    </form>
    </div>

{% endblock %}

这里还需要注意:
表单里的name属性的值,需要在view层里使用request.POST.get获取。
get()里面放的就是你表单属性的name值。
对于多选的情况,需要使用request.POST.getlist
image
还有就是创建新书籍之后,相当于已经添加了一对多关系。此时还需要再添加一下多对多关系。
关于redirect的重定向:自带反向解析 可以解析没有实现动态匹配的路由(传参数)
image

书籍编辑页

image

编辑页和添加页的不同就是,编辑页需要默认值。
这些默认值来自于你选择的书籍对象。
对于书籍编辑页的路由,需要使用动态反向解析。
如何匹配:book_edit/<int:book_pk>/
如:127.0.0.1:8000/edit_book/1/
多选框的默认选中:
对于input是checked
对于option是selected

{% extends 'home_page.html' %}

{% block panel-body %}
    <div class="col-md-8 col-md-offset-2" >
        <h2 class="text-center">图书编辑页</h2>
        <form action="" method="post">
        <p>书名:
            <input type="text" name="name"  value="{{ edit_obj.name}}" class="form-control">
        </p>
        <p>价格:
            <input type="text" name="price" value="{{ edit_obj.price }}" class="form-control">
        </p>
        <p>出版时间:
            <input type="date" name="publish_time" value="{{ edit_obj.publish_time|date:'Y-m-d' }}" class="form-control">
        </p>
        <p>出版社:
            <select name="publish_id" class="form-control">
                {% for publish_obj in publish_queryset %}

                    {% if publish_obj.pk == edit_obj.pk %}
                        <option value="{{ publish_obj.pk }}" selected>{{ publish_obj.name }}</option>
                    {% else %}
                        <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
                    {% endif %}

                {% endfor %}
            </select>
        </p>
        <p>作者:
            <select name="author_id_list" class="form-control" multiple>
                {% for author_obj in author_queryset %}
                    {% if author_obj in edit_obj.authors.all %}   <!--注意这里是对象和对象比-->
                        <option value="{{ author_obj.pk }}" selected>{{ author_obj.name }}</option>
                    {% else %}
                        <option value="{{ author_obj.pk }}" >{{ author_obj.name }}</option>
                    {% endif %}
                {% endfor %}
            </select>
        </p>
        <input type="submit" value="编辑书籍" class="btn btn-success">
    </form>
    </div>





{% endblock %}

书籍删除功能

删除书籍之后,记得删除第三张表的关联字段。
点击删除按钮之后,可以弹出一个模态框进行提示。使用return false,取消点击按钮的默认事件。
image

def book_delete_func(request, book_pk):
    # 删除第三张表中的数据 多对多必须先删 因为先删一对多 会导致没有书籍对象 无从删除。
    delete_book_obj = models.Book.objects.filter(pk=book_pk).first()
    delete_book_obj.authors.clear()
    # 删除书籍表中的数据
    models.Book.objects.filter(pk=book_pk).delete()

    return redirect('book_list_view')

view层

from django.shortcuts import render, HttpResponse, redirect, reverse
from app01 import models
# Create your views here.
def home_func(request):
    return render(request,'home_page.html')


def book_list_func(request, page_num):
    # 1.获取所有的图书数据
    book_queryset = models.Book.objects.all()
    page, mod = divmod(len(book_queryset), 5)
    print(page,mod)
    page_list = list(range(1, page+2))
    print(page_list)
    page_all_book = page_num * 5
    # 2.返回html页面并在页面上展示图书数据
    return render(request, 'book_list_page.html', locals())


def book_add_func(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        price = request.POST.get('price')
        publish_time = request.POST.get('publish_time')
        publish_id = request.POST.get('publish_id')  # 出版社只有一个
        author_id_list = request.POST.getlist('author_id_list')  # 作者是可以有多个的
        if all([name, price, publish_time, publish_id, author_id_list]):
            # 添加书籍 一对多
            book_obj = models.Book.objects.create(name=name,
                                                  price=price,
                                                  publish_time=publish_time,
                                                  publish_id=publish_id)
            # 添加第三张表 多对多
            book_obj.authors.add(*author_id_list)
            # 重定向
            return redirect('book_list_view')  # 只支持没有动态匹配的别名反向解析
            # 对于动态的要使用reserve
        else:
            return HttpResponse('添加失败')

    # 1.需要出版社和作者的数据
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()

    return render(request, 'book_add_page.html', locals())


def book_edit_func(request, book_pk):
    # 1.根据匹配到的数据主键值查询数据对象
    edit_obj = models.Book.objects.filter(pk=book_pk).first()  # 通过主键查询数据
    if request.method == 'POST':
        name = request.POST.get('name')
        price = request.POST.get('price')
        publish_time = request.POST.get('publish_time')
        publish_id = request.POST.get('publish_id')
        author_id_list = request.POST.getlist('author_id_list')
        models.Book.objects.filter(pk=book_pk).update(name=name,
                                                      price=price,
                                                      publish_time=publish_time,
                                                      publish_id=publish_id)
        # 多对多关系修改 update不会向create一样得到book_obj
        edit_obj.authors.set(author_id_list)
        return redirect('book_list_view')  # 对于不是动态匹配的路由 可以直接反向解析

    # 3.获取出版社、作者信息
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    # 2.传递给html页面并展示给用户看
    return render(request, 'book_edit_page.html', locals())


def book_delete_func(request, book_pk):
    # 删除第三张表中的数据 多对多必须先删 因为先删一对多 会导致没有书籍对象 无从删除。
    delete_book_obj = models.Book.objects.filter(pk=book_pk).first()
    delete_book_obj.authors.clear()
    # 删除书籍表中的数据
    models.Book.objects.filter(pk=book_pk).delete()

    return redirect('book_list_view')

路由分发

from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home_func, name='home_view'),
    path('book_list/<int:page_num>/', views.book_list_func, name='book_list_view'),
    path('book_add/', views.book_add_func, name='book_add_view'),
    path('book_edit/<int:book_pk>/',views.book_edit_func,name='book_edit_view'),
    path('book_delete/<int:book_pk>/', views.book_delete_func, name='book_delete_view'),
]

posted @ 2022-12-18 18:10  passion2021  阅读(97)  评论(0编辑  收藏  举报