Day66-图书管理系统-01

“Day66 图书管理系统(一) 学习笔记”

今日内容概述

  • 项目准备
  • 图书管理–图书增删改查
  • choices参数(数据库字段设计常见)
  • MTV与MVC模型
  • 多对多关系的三种创建方式
  • Ajax操作(重点)
  • 前后端传输数据编码格式
  • ajax发送json格式数据
  • ajax发送文件
  • django自带的序列化功能(了解,后面讲路飞学城的时候会将更加牛逼的序列化drf)
  • ajax结合sweetalert实现删除按钮的二次确认操作

一、项目准备

1.1 创建day66_BMS项目

image-20221117104513853

1.2 创建数据库

mysql> create database day66;
Query OK, 1 row affected (0.01 sec)

1.3 配置连接数据库

编辑app01/init.,py文件

import pymysql
pymysql.install_as_MySQLdb()

编辑settings.py文件

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'day66',
        'HOST':'127.0.0.1',
        'PORT':3306,
        'USER':'root',
        'PASSWORD':'123123',
        'CHARSET':'utf8'
    }
}

1.4 创建数据表模型

编辑models.py文件

from django.db import models

# Create your models here.

# 创建数据表对象
class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=8,decimal_places=2)
    publish_date = models.DateField(auto_now_add=True)
    # 一对多
    publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
    # 多对多
    authors = models.ManyToManyField(to='Author')

    def __str__(self):
        return self.title


class Publish(models.Model):
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    email = models.EmailField()
    # varchar(254)  该字段类型不是给models看的 而是给后面我们会学到的校验性组件看的

    def __str__(self):
        return '对象:%s'%self.name


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    # 一对一
    author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)


class AuthorDetail(models.Model):
    phone = models.BigIntegerField()  # 电话号码用BigIntegerField或者直接用CharField
    addr = models.CharField(max_length=64)

执行同步数据库操作

python .\manage.py makemigrations
python .\manage.py migrate

确保数据表已经创建

mysql> show tables;
+----------------------------+
| Tables_in_day66            |
+----------------------------+
| app01_author               |
| app01_authordetail         |
| app01_book                 |
| app01_book_authors         |
| app01_publish              |
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
15 rows in set (0.00 sec)

1.5 数据库添加数据

使用navicat给数据库添加数据

# 作者信息表
mysql> select * from app01_authordetail;
+----+-------+------+
| id | phone | addr |
+----+-------+------+
|  1 | 11111 | 一区 |
|  2 | 22222 | 二区 |
|  3 | 33333 | 二区 |
|  4 | 44444 | 四区 |
+----+-------+------+

# 作者表
mysql> select * from app01_author;
+----+--------+-----+------------------+
| id | name   | age | author_detail_id |
+----+--------+-----+------------------+
|  1 | 老一头 |  11 |                1 |
|  2 | 老二头 |  22 |                2 |
|  3 | 老三头 |  33 |                3 |
|  4 | 老四头 |  44 |                4 |
+----+--------+-----+------------------+

# 出版社表
mysql> select * from app01_publish;
+----+------------+------+------------+
| id | name       | addr | email      |
+----+------------+------+------------+
|  1 | 浪迹出版社 | 北京 | [email protected] |
|  2 | 天涯出版社 | 深圳 | [email protected] |
|  3 | 落花出版社 | 上海 | 456@qq,com |
|  4 | 流水出版社 | 广州 | 678@qq,com |
+----+------------+------+------------+

# 书表
mysql> select * from app01_book;
+----+----------+--------+--------------+------------+
| id | title    | price  | publish_date | publish_id |
+----+----------+--------+--------------+------------+
|  1 | 红楼梦   | 128.00 | 2022-11-17   |          3 |
|  2 | 水浒传   |  68.00 | 2022-11-01   |          2 |
|  3 | 西游记   | 200.00 | 2022-11-02   |          1 |
|  4 | 三国演义 |  60.00 | 2022-11-18   |          4 |
+----+----------+--------+--------------+------------+

# 书—作者表
mysql> select * from app01_book_authors;
+----+---------+-----------+
| id | book_id | author_id |
+----+---------+-----------+
|  1 |       1 |         2 |
|  2 |       1 |         3 |
|  4 |       2 |         1 |
|  7 |       2 |         2 |
|  3 |       2 |         4 |
|  5 |       3 |         3 |
|  6 |       4 |         2 |
+----+---------+-----------+

1.6 添加首页

编辑views.py, 定义页面view

from django.shortcuts import render


# Create your views here.
def home(request):
    return render(request, 'home.html')

编辑urls.py添加解析

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

urlpatterns = [
    path('admin/', admin.site.urls),
    # 首页
    path(r'^S', views.home, name='home')
]

新建文件template/home.html添加首页

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    {% block css %}

    {% 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="#">图书管理系统</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="#">图书 <span class="sr-only">(current)</span></a></li>
        <li><a href="#">作者</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多 <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="#">Jason</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多操作 <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-3">
                <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-9">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">BMS</h3>
          </div>
          <div class="panel-body">
              {% block content %}
                <div class="jumbotron">
                  <h1>欢迎来到亚洲最大的线上读书平台!</h1>
                  <p>这里有的没有的书籍都有</p>
                  <p><a class="btn btn-primary btn-lg" href="#" role="button">点击有你好看哟~</a></p>
                </div>
                <div class="row">
                  <div class="col-sm-6 col-md-4">
                    <div class="thumbnail">
                      <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABsbGxscGx4hIR4qLSgtKj04MzM4PV1CR0JHQl2NWGdYWGdYjX2Xe3N7l33gsJycsOD/2c7Z//////////////8BGxsbGxwbHiEhHiotKC0qPTgzMzg9XUJHQkdCXY1YZ1hYZ1iNfZd7c3uXfeCwnJyw4P/Zztn////////////////CABEIALkAsAMBIgACEQEDEQH/xAAZAAEAAwEBAAAAAAAAAA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qT8vOND2YXp5I4gutgoOwJxXrwJpRGVjhUTenlRDYneklRzYHemJAq5jVndsdWDifTGhxdaSSJt1cVO5Z7dhVj571AxZCp7VpWNGKKo27C1G5796Fx3p50RAzZIwKmneY+FGB1gSCCMigwk3HxdxQBJsBUMZRd8miLgg08LqcEiiNIu+wqRzI1/2H6AEjcVHxbrswvXqeVr1B4NF3ONqZ2c3Y/oFR2wpNDh3OSBUUSxsGO5BBoxRSjUO/cUeFHZzQgjTd2qeJJXd12JYmjw7jBBpkdcqR1FUsQAN6SBV3O55kUkrx4JFfm5bfEtPKz5YtQvk83hRsbGiCpIPSij0L8zn3WHj3cQlxrHbPRgTU1zheXenmKOVKgi1QyFw1wBbo52NOuhivQjTQgHLualj9QC2RSIEWw957fXnxC4b/AIffAuqQeBvzGT0W5uupWXyPfw62S/nmvRbB9ky6ZG90X9tPtHNcdFvhPs4j4x9vP//EACQRAAEEAAUFAQEAAAAAAAAAAAEAAgMREBIgMVETISJAQTBx/9oACAECAQE/APYuiVYRl4C6rkJT9CzBXZ0uqu5RcKTbBFbrIeU6yTe6Dk1zdB7AogEcuwjYdygGqSP6DhldVGtWS3nhDArp+YI9PuP4swVk7LbW6XhGRx+oTRncG0+W6yighK5NeHaZXfEGk7IijWhpykHQTQJRNlAkGwib0xG21jKfH8Ij5Yf/xAAiEQEAAgEEAgIDAAAAAAAAAAABAAIREBIgITAxA0ETQFH/2gAIAQMBAT8A/YVraFiN5vYXm8gtnjYNHQ0rg49J/XRY7vqVX7NMOMcrdMdCVTPfOwmcw+PJ3ERxPxmIVXwZQ6jlcytrPgbRVmy56t1KFhyuWb2CPGz9QM8Rw8HwVetbevBX3p//2Q==" 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="data:image/j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qjix4KsEbfacJj8jhsasKZhZm7EDtBfPrPiROtAe1w9BOprvGFwbiAXNDdvucRWij1M1Mtm4mda/iKaEGXXqYiuoH0hbHlATImoA7RuE4ZrCsymP8Ly3aOrQ8FxQoeTF4E882UL/avqMxomEEYl0tys7tGQPpgGkaVAAEyjS3uJ8T/nIP/WJzaLBKINw33M37nxHOcZyWPyPvPwz8OL/ExOkf5T7ROSQ/Pm9x+0PziL8x95+IewmT+Yv+v7zP8yz4j/5X+iwdZ0nfw6eH/8QAIBEAAgICAgMBAQAAAAAAAAAAAAEQEQIxEiADITAiUf/aAAgBAgEBPwC0ckN2NyoxmizF7LvLsnDh6E6TMdXH5qErH6ELK2y40Nl/CoyafwU0yyhYIfiv2j2vThmGNs8mPFwoeAsEUaPIoowVGauEy10Yziv4UhaGZbin8ctjRxZxUWWtX2fS44PnY5UPqtxQ1QkP0eJ/tdf/xAAiEQACAgICAwEAAwAAAAAAAAAAAQIREDESIQMgQTIiYnH/2gAIAQMBAT8AooSF6telHkSVUcUoW9+zGLC2NXJHk7aQju8N0LtDHFJJji18x+kJHGu/uH6Ltl0l1bHL58P4kLWF60iWui7Ol2zmhLEpti8qj0yLT/xnH+w6JyqLPFLlHvEnhTHN/Bm2ePVZ8jtM8Tq8NWcXmWhbNHJlkn2Q+i1i1mRFesNMTo5L4jk8UNNkdZeniOvbkuFCeW+hojperxYnY3SF3sn+GLQ8/wD/2Q==" 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="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABsbGxscGx4hIR4qLSgtKj04MzM4PV1CR0JHQl2NWGdYWGdYjX2Xe3N7l33gsJycsOD/2c7Z//////////////8BGxsbGxwbHiEhHiotKC0q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xK/wBLTU/xD+8fiL4R7TTeA+8PQejU/mf2w+Nffo03jaN4m9z2f//EACMRAAICAQQBBQEAAAAAAAAAAAECABEgAxASMSETIjBBUlH/2gAIAQIBAT8A+YtUszz/AGWYGyZqyVqNHEmzLly9zFNjc9ZXDE6wIo5fUTr49M94MKO1SjvRMVeIwdbnUsQnZF+8+HLdasSqyIZlPHuaeo+ixVwanp6ep5Vp6B/c4pp+WaFn1W9olEAX3ije7jX1cdF1BRjDgxAM5N+jNNebUTEQKKEJtyN//8QAIhEAAQQCAgEFAAAAAAAAAAAAAQACESAQIRJBMQMTMFFh/9oACAEDAQE/APmAUBaUBEWAsRUDShQoyEcjzYjDvNG7UV7TvNO8RRokr1gBFGGW4kqUMBwG09/M0Y6McSoAw93Vw4jLuXVxEiUWh4kLk5uiF7n4pc/QCADBtGJ1WNSmuLTIQMhQPpOPEIkk7RGhn//Z" 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>
              {% endblock %}

          </div>
        </div>

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

{% block js %}

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

运行后的界面如下:

image-20221117144310521

二、图书管理系统的增删改查

2.1 图书列表展示页面

实现功能:点击“图书列表”展示当前已有的图书

1、编辑urls.py添加url

urlpatterns = [
    path('admin/', admin.site.urls),
    # 首页
    path('', views.home, name='home'),
    # 添加图书展示页
    path('book/list/', views.book_list, name="book_list")
]

2、编辑views.py添加view

from django.shortcuts import render
from app01 import models  # 导入模块


# Create your views here.
def home(request):
    return render(request, 'home.html')

# 创建书籍展示页的view
def book_list(request):
    # 先查询出所有的书籍信息 传递给html页面
    book_queryset = models.Book.objects.all()
    return render(request, 'book_list.html', locals())

3、创建书籍展示页的html文件templates/book_list.html

{#继承home页面#}
{% extends 'home.html' %}

{#重写已定义的content块#}
{% block content %}
{#    添加按钮#}
    <a href="" class="btn btn-success">添加</a>
    <br> {#    添加空格#}
    <br>
{#    创建表格,使用bootstrp的模板#}
    <table class="table table-hover tabular-stripped">
        <thead>
        <tr>
            <th>ID</th>
            <th>书名</th>
            <th>价格</th>
            <th>出版日期</th>
            <th>出版社</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
            {% for book_obj in book_queryset %}
                <tr>
                    <td>{{ book_obj.pk }}</td>
                    <td>{{ book_obj.title }}</td>
                    <td>{{ book_obj.price }}</td>
                    <td>{{ book_obj.publish_date | date:'Y-m-d'}}</td>
                    <td>{{ book_obj.publish.name }}</td>
                    <td>
                        {% for author_obj in book_obj.authors.all %}
                            {% if author_obj.last %}
                                {{ author_obj.name }}
                            {% else %}
                                {{ author_obj.name }}、
                            {% endif %}

                        {% endfor %}
                    </td>
                    <td>
                        <a href="" class="btn btn-primary btn-sm">编辑</a>
                        <a href="" class="btn btn-danger btn-sm">删除</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}

4、给图书列表按钮添加链接,修改home.html文件

# 75行
<a href="#" class="list-group-item">图书列表</a>
# 改为
<a href="{% url 'book_list' %}" class="list-group-item">图书列表</a>

5、点击图书列表查看效果

image-20221117151215258

2.2 书籍的添加

2.3 书籍的编辑

2.4 书籍的删除

三、Choies参数

四、MTV与MVC模型

五、多对多的三种创建方式

六、Ajax


Day66-图书管理系统-01
http://gsproj.github.io/2022/11/17/03_Python/05_Django/day66笔记-图书管理系统-01/
作者
GongSheng
发布于
2022年11月17日
许可协议