zoukankan      html  css  js  c++  java
  • Django之一个基于多表的图书管理系统

    准备工作参考单表,这里只贴出关键代码,项目结构与基于单表的木有区别.

    1.url控制器

    urls.py
      1: from django.contrib import admin
    
      2: from django.urls import path,re_path
    
      3: from app01 import views
    
      4: 
    
      5: urlpatterns = [
    
      6:     path('admin/', admin.site.urls),
    
      7:     path('book/add', views.add_book),
    
      8:     path('book/search', views.search_book),
    
      9:     re_path('book/(d+)/change', views.change_book),
    
     10:     re_path('book/(d+)/delete', views.delete_book),
    
     11: ]

    2.模型层

    models.py

      1: from django.db import models
    
      2: # Create your models here.
    
      3: class Author(models.Model):
    
      4:     nid = models.AutoField(primary_key=True)
    
      5:     name = models.CharField(max_length=32)
    
      6:     age = models.IntegerField()
    
      7: class Publish(models.Model):
    
      8:     nid = models.AutoField(primary_key=True)
    
      9:     name = models.CharField(max_length=32)
    
     10:     city = models.CharField(max_length=32)
    
     11:     email = models.EmailField()
    
     12: class Book(models.Model):
    
     13:     nid = models.AutoField(primary_key=True)
    
     14:     title = models.CharField(max_length=32)
    
     15:     publishDate = models.DateField()
    
     16:     price = models.DecimalField(max_digits=5, decimal_places=2)
    
     17:     publish = models.ForeignKey(to="Publish", to_field="nid", on_delete=models.CASCADE)
    
     18:     authors = models.ManyToManyField(to="Author",)
    
     19: 

    3.视图层

    views.py

      1: from django.shortcuts import render, HttpResponse, redirect
    
      2: 
    
      3: # Create your views here.
    
      4: from .models import Publish, Author, Book
    
      5: 
    
      6: 
    
      7: def add_book(request):
    
      8: 
    
      9:     if request.method == "POST":
    
     10:         title = request.POST.get("title")
    
     11:         price = request.POST.get("price")
    
     12:         pub_date = request.POST.get("pub_date")
    
     13:         publish_id = request.POST.get("publish_id")
    
     14:         authors_id_list = request.POST.getlist("authors_id_list")
    
     15:         # print(authors_id_list)
    
     16: 
    
     17:         book_obj = Book.objects.create(title=title, price=price, publishDate=pub_date, publish_id=publish_id)
    
     18:         book_obj.authors.add(*authors_id_list)
    
     19: 
    
     20:         return redirect("/book/search")
    
     21: 
    
     22:     publish_list = Publish.objects.all()
    
     23:     author_list = Author.objects.all()
    
     24:     return render(request, "add_book.html", {"publish_list": publish_list, "author_list": author_list})
    
     25: 
    
     26: 
    
     27: def search_book(request):
    
     28: 
    
     29:     book_list = Book.objects.all()
    
     30: 
    
     31:     return render(request, "search_book.html", {"book_list": book_list})
    
     32: 
    
     33: 
    
     34: def change_book(request, edit_book_id):
    
     35: 
    
     36:     edit_book_obj = Book.objects.filter(pk=edit_book_id)[0]
    
     37:     if request.method == "POST":
    
     38:         title = request.POST.get("title")
    
     39:         price = request.POST.get("price")
    
     40:         pub_date = request.POST.get("pub_date")
    
     41:         publish_id = request.POST.get("publish_id")
    
     42:         authors_id_list = request.POST.getlist("authors_id_list")
    
     43: 
    
     44:         Book.objects.filter(pk=edit_book_id).update(title=title, price=price, publishDate=pub_date, publish_id=publish_id)
    
     45:         # edit_book_obj.authors.clear()
    
     46:         # edit_book_obj.authors.add(*authors_id_list)
    
     47:         # 同上的写法
    
     48:         edit_book_obj.authors.set(authors_id_list)
    
     49: 
    
     50:         return redirect("/book/search")
    
     51: 
    
     52:     publish_list = Publish.objects.all()
    
     53:     author_list = Author.objects.all()
    
     54:     return render(request, "editbook.html", {"edit_book_obj": edit_book_obj, "publish_list": publish_list, "author_list": author_list})
    
     55: 
    
     56: 
    
     57: def delete_book(request, delete_book_id):
    
     58: 
    
     59:     Book.objects.filter(pk=delete_book_id).delete()
    
     60: 
    
     61:     return redirect("/book/search")

    4.模板层

    templates

    add_book.html

      1: <!DOCTYPE html>
    
      2: <html lang="en">
    
      3: <head>
    
      4:     <meta charset="UTF-8">
    
      5:     <title>添加页面</title>
    
      6:     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
      7: </head>
    
      8: <body>
    
      9: <div>
    
     10:     <h3>添加书籍</h3>
    
     11: </div>
    
     12: 
    
     13: <div class="container">
    
     14:     <div class="row">
    
     15:         <div class="col-md-6 col-md-offset-3">
    
     16:             <form action="" method="post">
    
     17:                 {% csrf_token %}
    
     18:                 <div class="from-group">
    
     19:                     <label for="">名称</label>
    
     20:                     <input type="text" name="title" class="form-control">
    
     21:                 </div>
    
     22:                 <div class="from-group">
    
     23:                     <label for="">价格</label>
    
     24:                     <input type="text" name="price" class="form-control">
    
     25:                 </div>
    
     26:                 <div class="from-group">
    
     27:                     <label for="">出版日期</label>
    
     28:                     <input type="date" name="pub_date" class="form-control">
    
     29:                 </div>
    
     30:                 <div class="from-group">
    
     31:                     <label for="">出版社</label>
    
     32:                     <select name="publish_id" id="" class="form-control">
    
     33:                         {% for publish in publish_list %}
    
     34:                             <option value="{{ publish.pk }}">{{ publish.name }}</option>
    
     35:                         {% endfor %}
    
     36:                     </select>
    
     37:                 </div>
    
     38:                 <div class="from-group">
    
     39:                     <label for="">作者</label>
    
     40:                     <select type="text" name="authors_id_list" id="" multiple class="form-control">
    
     41:                         {% for author in author_list %}
    
     42:                             <option value="{{ author.pk }}">{{ author.name }}</option>
    
     43:                         {% endfor %}
    
     44:                     </select>
    
     45:                 </div>
    
     46:                 <div>
    
     47:                     <input type="submit" class="btn btn-default">
    
     48:                 </div>
    
     49:             </form>
    
     50:         </div>
    
     51:     </div>
    
     52: </div>
    
     53: </body>
    
     54: </html>

    editbook.html

      1: <!DOCTYPE html>
    
      2: <html lang="en">
    
      3: <head>
    
      4:     <meta charset="UTF-8">
    
      5:     <title>添加页面</title>
    
      6:     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
      7: </head>
    
      8: <body>
    
      9: <div>
    
     10:     <h3>编辑书籍</h3>
    
     11: </div>
    
     12: 
    
     13: <div class="container">
    
     14:     <div class="row">
    
     15:         <div class="col-md-6 col-md-offset-3">
    
     16:             <form action="" method="post">
    
     17:                 {% csrf_token %}
    
     18:                 <div class="from-group">
    
     19:                     <label for="">名称</label>
    
     20:                     <input type="text" name="title" class="form-control" value="{{ edit_book_obj.title }}">
    
     21:                 </div>
    
     22:                 <div class="from-group">
    
     23:                     <label for="">价格</label>
    
     24:                     <input type="text" name="price" class="form-control" value="{{ edit_book_obj.price }}">
    
     25:                 </div>
    
     26:                 <div class="from-group">
    
     27:                     <label for="">出版日期</label>
    
     28:                     <input type="date" name="pub_date" class="form-control" value="{{ edit_book_obj.publishDate|date:"Y-m-d" }}">
    
     29:                 </div>
    
     30:                 <div class="from-group">
    
     31:                     <label for="">出版社</label>
    
     32:                     <select name="publish_id" id="" class="form-control">
    
     33:                         {% for publish in publish_list %}
    
     34:                             {% if edit_book_obj.publish == publish %}
    
     35:                                 <option selected value="{{ publish.pk }}">{{ publish.name }}</option>
    
     36:                             {% else %}
    
     37:                                 <option value="{{ publish.pk }}">{{ publish.name }}</option>
    
     38:                             {% endif %}
    
     39:                         {% endfor %}
    
     40:                     </select>
    
     41:                 </div>
    
     42:                 <div class="from-group">
    
     43:                     <label for="">作者</label>
    
     44:                     <select type="text" name="authors_id_list" id="" multiple class="form-control">
    
     45:                         {% for author in author_list %}
    
     46:                             {% if author in edit_book_obj.author.all %}
    
     47:                                 <option selected value="{{ author.pk }}">{{ author.name }}</option>
    
     48:                             {% else %}
    
     49:                                 <option value="{{ author.pk }}">{{ author.name }}</option>
    
     50:                             {% endif %}
    
     51:                         {% endfor %}
    
     52:                     </select>
    
     53:                 </div>
    
     54:                 <div>
    
     55:                     <input type="submit" class="btn btn-default">
    
     56:                 </div>
    
     57:             </form>
    
     58:         </div>
    
     59:     </div>
    
     60: </div>
    
     61: </body>
    
     62: </html>

    search_book.html

      1: <!DOCTYPE html>
    
      2: <html lang="en">
    
      3: <head>
    
      4:     <meta charset="UTF-8">
    
      5:     <title>查看页面</title>
    
      6:     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
      7: </head>
    
      8: <body>
    
      9: <div>
    
     10:     <h3>查看书籍</h3>
    
     11: </div>
    
     12: <div>
    
     13:     <h3>添加书籍</h3>
    
     14:     <a href="/book/add" class="btn btn-info">添加</a>
    
     15: </div>
    
     16: <div class="container">
    
     17:     <div class="row">
    
     18:         <div class="col-md-8 col-md-offset-3">
    
     19:             <table class="table table-bordered table-hover table-striped" >
    
     20:                 <thead>
    
     21:                     <tr>
    
     22:                         <th>编号</th>
    
     23:                         <th>书籍名称</th>
    
     24:                         <th>价格</th>
    
     25:                         <th>出版日期</th>
    
     26:                         <th>出版社</th>
    
     27:                         <th>作者</th>
    
     28:                         <th>操作</th>
    
     29:                     </tr>
    
     30:                 </thead>
    
     31:                 <tbody>
    
     32:                     {% for book in book_list %}
    
     33:                     <tr>
    
     34:                         <td>{{ forloop.counter }}</td>
    
     35:                         <td>{{ book.title }}</td>
    
     36:                         <td>{{ book.price }}</td>
    
     37:                         <td>{{ book.publishDate|date:"Y-m-d" }}</td>
    
     38:                         <td>
    
     39:                             {{ book.publish.name }}
    
     40:                         </td>
    
     41:                         <td>
    
     42:                             {% for author in book.authors.all %}
    
     43:                                 {% if forloop.last %}
    
     44:                                     <span>{{ author.name }}</span>
    
     45:                                 {% else %}
    
     46:                                     <span>{{ author.name }}.</span>
    
     47:                                 {% endif %}
    
     48:                             {% endfor %}
    
     49:                         </td>
    
     50:                         <td>
    
     51:                             <a href="/book/{{ book.pk }}/change" class="btn btn-warning">编辑</a>
    
     52:                             <a href="/book/{{ book.pk }}/delete" class="btn btn-danger">删除</a>
    
     53:                         </td>
    
     54:                     </tr>
    
     55:                     {% endfor %}
    
     56:                 </tbody>
    
     57:             </table>
    
     58:         </div>
    
     59:     </div>
    
     60: </div>
    
     61: </body>
    
     62: </html>
  • 相关阅读:
    使用golang访问kubebernetes
    使用 Rancher 管理现有 Kubernetes 集群
    Running powershell scripts during nuget package installation and removal
    How to Create, Use, and Debug .NET application Crash Dumps in 2019
    寻找写代码感觉(一)之使用 Spring Boot 快速搭建项目
    Selenium+Java之解决org.openqa.selenium.InvalidArgumentException: invalid argument报错问题
    Selenium环境搭建
    关于Xpath定位方法知道这些基本够用
    Web自动化之浏览器启动
    【翻译】编写代码注释的最佳实践
  • 原文地址:https://www.cnblogs.com/haoqirui/p/10176586.html
Copyright © 2011-2022 走看看