模型层_多表练习
基于book系统,加上出版社和作者字段,如下:
html模板层
1 <div class="action"> 2 <div class="panel panel-danger"> 3 <div class="panel-heading">2019进击的菜鸟</div> 4 <div class="panel-body"> 5 web框架开发 6 </div> 7 <div class="panel-body"> 8 crm&爬虫 9 </div> 10 <div class="panel-body"> 11 算法&设计模式&企业应用 12 </div> 13 <div class="panel-body"> 14 vue项目 15 </div> 16 <div class="panel-body"> 17 复习python&自动化&性能 18 </div> 19 </div> 20 <div class="panel panel-warning"> 21 <div class="panel-heading">2020进击的小鸟</div> 22 <div class="panel-body"> 23 fighting! 24 </div> 25 </div> 26 <div class="panel panel-success"> 27 <div class="panel-heading">2021进击的大鸟</div> 28 <div class="panel-body"> 29 go on ! 30 </div> 31 </div> 32 </div>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 {% block title %} 6 <title>base——title</title> 7 {% endblock title %} 8 <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" 9 integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 10 {% block style %} 11 <style type="text/css"> 12 *{ 13 padding: 0; 14 margin:0; 15 } 16 .header{ 17 width:100%; 18 height: 50px; 19 background-color: #369; 20 } 21 body{ 22 background:#FFF url('/static/base.jpg') repeat-x 0; 23 background-attachment: fixed; 24 } 25 </style> 26 {% endblock style %} 27 28 </head> 29 <body> 30 31 <div class="header"></div> 32 33 <div class="container"> 34 <div class="row"> 35 <div class="col-md-3"> 36 {% include 'advertise.html' %} 37 </div> 38 <div class="col-md-9"> 39 {% block content%} 40 <h3>base_content</h3> 41 {% endblock %} 42 </div> 43 </div> 44 </div> 45 46 </body> 47 {% block js %} 48 <script type="text/javascript" src="/static/jquery-3.3.1.js"></script> 49 {% endblock js %} 50 </html>
1 {% extends 'base.html' %} 2 3 {% block style %} 4 {{ block.super }} 5 <style> 6 .mybook { 7 margin-top: 40px; 8 } 9 10 .btn { 11 margin-top: 5px; 12 } 13 .other{ 14 margin-top: 5px; 15 } 16 .filter{ 17 margin-bottom: 30px; 18 } 19 .empty{ 20 text-align: center; 21 color: #D0D0D0; 22 } 23 th,td{ 24 text-align: center; 25 } 26 </style> 27 {% endblock style %} 28 29 {% block title %} 30 <title>book</title> 31 {% endblock title %} 32 33 {% block content %} 34 <h3>查看书籍</h3> 35 36 <div class="container mybook"> 37 <div class="row"> 38 <div class="col-md-7"> 39 <form class="form-inline filter" action=""> 40 <div class="form-group"> 41 <div class="form-group other"> 42 <label for="">书籍名称</label> 43 <input type="text" class="form-control" placeholder="python" name="title"> 44 </div> 45 </div> 46 <button type="submit" class="btn btn-primary">查询</button> 47 </form> 48 <a href="/app01/book/add" class="btn btn-primary other">添加书籍</a> 49 <table class="table table-striped table-bordered"> 50 <thead> 51 <tr> 52 <th>书籍名称</th> 53 <th>价格</th> 54 <th>出版日期</th> 55 <th>出版社</th> 56 <th>作者</th> 57 <th>操作</th> 58 </tr> 59 </thead> 60 <tbody> 61 {% for book in book_list %} 62 <tr> 63 <td>{{ book.title }}</td> 64 <td>{{ book.price }}</td> 65 <td>{{ book.pub_date|date:'Y-m-d' }}</td> 66 <td>{{ book.publish.name }}</td> 67 <td> 68 {% for author in book.authors.all %} 69 {% if forloop.last %} 70 <span>{{ author.name }}</span> 71 {% else %} 72 <span>{{ author.name }}</span>, 73 {% endif %} 74 {% endfor %} 75 </td> 76 <td> 77 <a href="/app01/book/{{ book.pk }}/update" class="btn btn-info">编辑</a> 78 <a href="/app01/book/{{ book.pk }}/delete" class="btn btn-danger">删除</a> 79 </td> 80 </tr> 81 {% empty %} 82 <td class="empty" colspan="6">暂无数据!</td> 83 {% endfor %} 84 85 </tbody> 86 87 </table> 88 89 </div> 90 </div> 91 </div> 92 {% endblock content %}
1 {% extends 'base.html' %} 2 3 {% block style %} 4 {{ block.super }} 5 <style> 6 .mybook { 7 margin-top: 50px; 8 } 9 10 .btn { 11 margin-top: 10px; 12 } 13 14 .opt_res { 15 margin-top: 50px; 16 color: red; 17 } 18 </style> 19 {% endblock style %} 20 21 {% block title %} 22 <title>book</title> 23 {% endblock title %} 24 25 {% block content %} 26 <h3>新增书籍</h3> 27 28 <div class="container mybook"> 29 <div class="row"> 30 <div class="col-md-6"> 31 <form action="" method="post"> 32 {% csrf_token %} 33 <div class="form-group"> 34 <label for="">书籍名称</label> 35 <input type="text" class="form-control" name="title" value={{ title }}> 36 </div> 37 <div class="form-group"> 38 <label for="">价格</label> 39 <input type="text" class="form-control" name="price" value={{ price }}> 40 </div> 41 <div class="form-group"> 42 <label for="">出版日期</label> 43 <input type="date" class="form-control" name="date" value={{ date }}> 44 </div> 45 <div class="form-group"> 46 <label for="">出版社</label> 47 <select name="publish_id" id="" class="form-control"> 48 {% for publish in publish_obj %} 49 <option value={{ publish.pk }}>{{ publish.name }}</option> 50 {% endfor %} 51 </select> 52 </div> 53 <div class="form-group"> 54 <label for="">作者</label> 55 <select type="text" name="authors_id_list" multiple class="form-control"> 56 {% for author in author_obj %} 57 <option value="{{ author.pk }}">{{ author.name }}</option> 58 {% endfor %} 59 </select> 60 </div> 61 <input type="submit" class="btn btn-success pull-right"> 62 </form> 63 <p class="opt_res">{{ opt_res }}</p> 64 </div> 65 </div> 66 </div> 67 {% endblock content %}
1 {% extends 'base.html' %} 2 3 {% block style %} 4 {{ block.super }} 5 <style> 6 .mybook { 7 margin-top: 50px; 8 } 9 10 .btn { 11 margin-top: 10px; 12 } 13 14 .opt_res { 15 margin-top: 50px; 16 color: red; 17 } 18 </style> 19 {% endblock style %} 20 21 {% block title %} 22 <title>book</title> 23 {% endblock title %} 24 25 {% block content %} 26 <h3>修改书籍</h3> 27 28 <div class="container mybook"> 29 <div class="row"> 30 <div class="col-md-6"> 31 <form action="" method="post"> 32 {% csrf_token %} 33 <div class="form-group"> 34 <label for="">书籍名称</label> 35 <input type="text" class="form-control" name="title" value="{{ book_obj.title }}"> 36 </div> 37 <div class="form-group"> 38 <label for="">价格</label> 39 <input type="text" class="form-control" name="price" value="{{ book_obj.price }}"> 40 </div> 41 <div class="form-group"> 42 <label for="">出版日期</label> 43 <input type="date" class="form-control" name="date" 44 value="{{ book_obj.pub_date|date:'Y-m-d' }}"> 45 </div> 46 <div class="form-group"> 47 <label for="">出版社</label> 48 <select name="publish_id" id="" class="form-control"> 49 {% for publish in publish_obj %} 50 {% if book_obj.publish == publish %} 51 <option value={{ publish.pk }} selected='True'>{{ publish.name }}</option> 52 {% else %} 53 <option value={{ publish.pk }}>{{ publish.name }}</option> 54 {% endif %} 55 {% endfor %} 56 </select> 57 </div> 58 <div class="form-group"> 59 <label for="">作者</label> 60 <select type="text" name="authors_id_list" multiple class="form-control"> 61 {% for author in author_obj %} 62 {% if author in book_obj.authors.all %} 63 <option value="{{ author.pk }}" selected="True">{{ author.name }}</option> 64 {% else %} 65 <option value="{{ author.pk }}">{{ author.name }}</option> 66 {% endif %} 67 {% endfor %} 68 </select> 69 </div> 70 <input type="submit" class="btn btn-success pull-right"> 71 </form> 72 <p class="opt_res">{{ opt_res }}</p> 73 </div> 74 </div> 75 </div> 76 {% endblock content %}
book控制器
06 django模型层ookookurls.py
from django.contrib import admin from django.urls import path,re_path,include urlpatterns = [ path('admin/', admin.site.urls), re_path('^app01/', include(('book_app01.urls','book_app01'))), ]
book_app01控制器
06 django模型层ookook_app01urls.py
from django.urls import path,re_path,include from book_app01 import views urlpatterns = [ path('book/', views.book), path('book/add', views.add_book), re_path(r'book/(d+)/update', views.update_book), re_path(r'book/(d+)/delete', views.delete_book), ]
book_app01模型层
06 django模型层ookbook_app01models.py
1 from django.db import models 2 3 # Create your models here. 4 5 6 class Publish(models.Model): 7 id = models.AutoField(primary_key=True) 8 name = models.CharField(max_length=32) 9 city = models.CharField(max_length=32) 10 email = models.EmailField() 11 12 def __str__(self): 13 return self.name 14 15 16 class Author(models.Model): 17 id = models.AutoField(primary_key=True) 18 name = models.CharField(max_length=32) 19 20 # 一对一 与AuthorDetail 21 authorDetail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE) 22 23 def __str__(self): 24 return self.name 25 26 27 class AuthorDetail(models.Model): 28 id = models.AutoField(primary_key=True) 29 age = models.BigIntegerField() 30 addr = models.CharField(max_length=32) 31 phone = models.BigIntegerField() 32 33 # def __str__(self): 34 # return self.phone 35 36 37 class Book(models.Model): 38 id = models.AutoField(primary_key=True) 39 title = models.CharField(max_length=32, unique=True) 40 price = models.DecimalField(max_digits=5, decimal_places=2) 41 pub_date = models.DateField() 42 read_num = models.BigIntegerField(default=0) 43 comment_num = models.BigIntegerField(default=0) 44 45 # 一对多 46 publish = models.ForeignKey(to='Publish', to_field='id', on_delete=models.CASCADE) 47 48 # 多对多 自动创建第三张表 book_authors 49 authors = models.ManyToManyField(to='Author',) 50 51 def __str__(self): 52 return self.title 53 54 55 class Employee(models.Model): 56 id = models.AutoField(primary_key=True) 57 name = models.CharField(max_length=32, unique=True) 58 age = models.BigIntegerField() 59 sal = models.DecimalField(max_digits=5, decimal_places=1) 60 dep = models.CharField(max_length=32) 61 62 def __str__(self): 63 return self.name
book_app01视图层
1 from django.shortcuts import render,redirect 2 from book_app01.models import * 3 4 # Create your views here. 5 6 7 def book(request): 8 method = request.method 9 req = request.POST if request.POST else request.GET 10 try: 11 title = req.get('title') if req.get('title') else '' 12 except exception as e: 13 title = '' 14 book_list = Book.objects.filter(title__contains=title) 15 16 return render(request, 'book.html', locals()) 17 18 19 def add_book(request): 20 method = request.method 21 res = Publish.objects.all().exists() 22 res_author = Author.objects.all().exists() 23 if not res: 24 Publish.objects.create(name='人民出版社', city='北京', email='123@qq.com') 25 Publish.objects.create(name='南京出版社', city='南京', email='456@qq.com') 26 Publish.objects.create(name='东京出版社', city='东京', email='789@qq.com') 27 if not res_author: 28 d1 = AuthorDetail.objects.create(age=18, addr='北京', phone=13537730001) 29 d2 = AuthorDetail.objects.create(age=28, addr='北京', phone=13667730001) 30 d3 = AuthorDetail.objects.create(age=38, addr='深圳', phone=13967730001) 31 d2 = AuthorDetail.objects.filter(id=2).first() 32 d3 = AuthorDetail.objects.filter(id=3).first() 33 Author.objects.create(name='杨一', authorDetail_id=1) 34 Author.objects.create(name='杨二', authorDetail_id=d2.id) 35 Author.objects.create(name='杨三', authorDetail=d3) 36 if method == 'POST': 37 req = request.POST 38 print('req---->', req) 39 title = req.get('title').strip() 40 price = req.get('price').strip() 41 date = req.get('date').strip() 42 publish_id = req.get('publish_id').strip() 43 # get只能取到authors_id_list列表的最后一个值,getlist取整个authors_id_list列表 44 authors_id_lis = req.getlist('authors_id_list') 45 print('authors_id_lis------->', authors_id_lis) 46 if title and price and date and publish_id and authors_id_lis: 47 select_res = Book.objects.filter(title=title).exists() 48 if not select_res: 49 publish_obj = Publish.objects.filter(id=publish_id).first() 50 # res = Book.objects.create(title=title, price=price, pub_date=date, publish=publish_obj) 51 res = Book.objects.create(title=title, price=price, pub_date=date, publish_id=publish_id) 52 res.authors.add(*authors_id_lis) 53 return redirect('/app01/book') 54 opt_res = '书籍【%s】已存在,请修改后提交!'%title 55 else: 56 opt_res = '输入不能为空,请修改后提交!' 57 publish_obj = Publish.objects.all() 58 author_obj = Author.objects.all() 59 return render(request, 'addbook.html', locals()) 60 61 62 def update_book(request, num): 63 book_obj = Book.objects.filter(id=num).first() 64 publish_obj = Publish.objects.all() 65 author_obj = Author.objects.all() 66 if book_obj: 67 method = request.method 68 if method == 'POST': 69 req = request.POST 70 print('req---->', req) 71 title = req.get('title').strip() 72 price = req.get('price').strip() 73 date = req.get('date').strip() 74 publish_id = req.get('publish_id').strip() 75 # get只能取到authors_id_list列表的最后一个值,getlist取整个authors_id_list列表 76 authors_id_lis = req.getlist('authors_id_list') 77 print('authors_id_lis------->', authors_id_lis) 78 if title and price and date and publish_id and authors_id_lis: 79 select_res = Book.objects.exclude(id=num).filter(title=title) 80 if not select_res: 81 Book.objects.filter(id=num).update(title=title, price=price, pub_date=date, publish_id=publish_id) 82 # book_obj.authors.clear() 83 # book_obj.authors.add(*authors_id_lis) 84 book_obj.authors.set(authors_id_lis) # 该方法等同于上面两个步骤 85 return redirect('/app01/book') 86 opt_res = '书籍【%s】已存在,请修改后提交!' % select_res[0].title 87 else: 88 opt_res = '输入不能为空,请修改后提交!' 89 return render(request, 'updatebook.html', locals()) 90 return redirect('/app01/book') 91 92 93 def delete_book(request, num=None): 94 book_obj = Book.objects.filter(id=num).first() 95 if book_obj: 96 book_obj.authors.clear() 97 book_obj.delete() 98 return redirect('/app01/book')