图书管理系统进阶---多表操作
models.py模型类
Book模型类下面写一个获取所有作者的函数
class Book(models.Model):
.....
def get_all_author(self):
# name_list = []
# for author in self.authors.all():
# name_list.append(author.name)
# name_list_srt = '+'.join(name_list)
# return name_list_srt
return ','.join([author.name for author in self.authors.all()])
book_list.html 展示页面
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
</head>
<body>
<div class="container">
<h1>书籍展示</h1>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<a href="{% url 'add' %}" class="btn btn-primary">添加书籍</a>
<table style="margin-top: 10px;" class="table table-hover table-striped table-bordered">
<thead>
<tr>
<th>序号</th>
<th>书名</th>
<th>价钱</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book_obj in book_obj_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.price }}</td>
<td>{{ book_obj.pub_date|date:'Y-m-d' }}</td>
<td>{{ book_obj.pub.name }}</td>
<td>
{{ book_obj.get_all_author }}
//两种方法都可以,上面这种是在Book模型类下面写了一个函数,然后直接调用就行了,下面这种是用后台传过来的book类对象操作第三张表for循环
</td>
{# <td>#}
{# {% for author in book_obj.authors.all %}#}
{# {{ author.name }}#}
{# {% if forloop.last %}#}
{##}
{# {% else %}#}
{# ,#}
{# {% endif %}#}
{##}
{# {% endfor %}#}
{# </td>#}
<td>
<a href="{% url 'edit' book_obj.pk %}" class="btn btn-warning">编辑</a>
<a href="{% url 'delete_book' book_obj.pk %}" class="btn btn-danger">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
add.html添加书籍
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
</head>
<body>
<div class="container">
<h1>添加书籍</h1>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="{% url 'add' %}" method="post">
{# <form action="{% url 'add_book' %}" method="post">#}
{# <form action="{% url 'add_book' 2020 %}" method="post">#}
<div class="form-group">
<label for="title">书籍名称</label>
<input type="text" class="form-control" id="title" placeholder="书籍名称" name="title">
</div>
<div class="form-group">
<label for="price">价格</label>
<input type="text" class="form-control" id="price" placeholder="书籍名称" name="price">
</div>
<div class="form-group">
<label for="pub_date">出版日期</label>
<input type="date" class="form-control" id="pub_date" placeholder="书籍名称" name="pub_date">
</div>
<div class="form-group">
<label for="publish">出版社</label>
<select name="pub_id" id="publish" class="form-control">
{% for publish in publish_list %}
<option value="{{ publish.id }}">{{ publish.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="authors">作者</label>
<select multiple name="authors" id="authors" class="form-control">
{% for author in author_list %}
<option value="{{ author.id }}">{{ author.name }}</option>
{% endfor %}
</select>
</div>
<button class="btn btn-success pull-right">提交</button>
</form>
</div>
</div>
</div>
</body>
</html>
edit.html 编辑
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
</head>
<body>
<div class="container">
<h1>编辑书籍</h1>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="{% url 'edit' old_book_obj.pk %}" method="post">
{# <form action="{% url 'add_book' %}" method="post">#}
{# <form action="{% url 'add_book' 2020 %}" method="post">#}
<div class="form-group">
<label for="title">书籍名称</label>
<input type="text" class="form-control" id="title" placeholder="书籍名称" name="title" value="{{ old_book_obj.title }}">
</div>
<div class="form-group">
<label for="price">价格</label>
<input type="text" class="form-control" id="price" placeholder="书籍名称" name="price" value="{{ old_book_obj.price }}">
</div>
<div class="form-group">
<label for="pub_date">出版日期</label>
<input type="date" class="form-control" id="pub_date" placeholder="书籍名称" name="pub_date" value="{{ old_book_obj.pub_date|date:'Y-m-d' }}">
</div>
<div class="form-group">
<label for="publish">出版社</label>
<select name="pub_id" id="publish" class="form-control">
{% for publish in publish_list %}
{% if old_book_obj.pub == publish %}
<option value="{{ publish.id }}" selected>{{ publish.name }}</option>
{% else %}
<option value="{{ publish.id }}">{{ publish.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="authors">作者</label>
<select multiple name="authors" id="authors" class="form-control">
{% for author in author_list %}
{% if author in old_book_obj.authors.all %}
<option value="{{ author.id }}" selected>{{ author.name }}</option>
{% else %}
<option value="{{ author.id }}" >{{ author.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<button class="btn btn-success pull-right">提交</button>
</form>
</div>
</div>
</div>
</body>
</html>
urls.py文件
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^book_list/', views.book_list, name='book_list'),
url(r'^add/', views.AddEdit.as_view(), name='add'),
url(r'^edit/(\d+)/', views.AddEdit.as_view(), name='edit'),
url(r'^delete_book/(\d+)/', views.delete_book, name='delete_book'),
url(r'^xx/', views.xx, name='xx'),
]
views.py文件
from django.shortcuts import HttpResponse, render, redirect
from app01 import models
from django.views import View
#展示
def book_list(request):
book_obj_list = models.Book.objects.all()
return render(request,'book_list.html',{'book_obj_list':book_obj_list})
#添加
class Add(View):
def get(self,request):
publish_list = models.Publish.objects.all()
author_list = models.Author.objects.all()
return render(request,'add.html',{'publish_list': publish_list, 'author_list': author_list})
def post(self,request):
#querydict类型都是不可变的,想修改,可用下面两种方式
#request.POST._mutable = True #方法一,让querydict可变
data = request.POST.copy() #第二种方法
xx = data.pop('authors')
book_obj = models.Book.objects.create(
**data.dict(),
)
#添加这本书和作者的多对多关系
book_obj.authors.add(*xx)
return redirect('book_list')
#编辑
class Edit(View):
def get(self,request,pk):
old_book_obj = models.Book.objects.get(pk=pk)
publish_list = models.Publish.objects.all()
author_list = models.Author.objects.all()
return render(request,'edit.html',{'old_book_obj': old_book_obj,'publish_list': publish_list,'author_list': author_list})
def post(self,request,pk):
old_book_obj = models.Book.objects.filter(pk=pk)
authors_id_list = request.POST.getlist('authors')
data = request.POST.dict()
data.pop('authors')
old_book_obj.update(
**data
)
old_book_obj.first().authors.set(authors_id_list)
return redirect('book_list')
#删除
def delete_book(request,pk):
models.Book.objects.filter(pk=pk).delete()
return redirect('book_list')
进阶优化代码,合并添加和编辑
class AddEdit(View):
def get(self,request,pk=None):
old_books = models.Book.objects.filter(pk=pk)
publish_list = models.Publish.objects.all()
author_list = models.Author.objects.all()
if old_books.exists():
old_book_obj = old_books.first()
return render(request,'add_edit.html',{'old_book_obj': old_book_obj,'publish_list': publish_list,'author_list': author_list})
else:
return render(request,'add_edit.html',
{'publish_list': publish_list, 'author_list': author_list})
def post(self,request,pk=None):
old_book_obj = models.Book.objects.filter(pk=pk)
authors_id_list = request.POST.getlist('authors')
data = request.POST.dict()
data.pop('authors')
if old_book_obj.exists():
old_book_obj.update(
**data
)
old_book_obj.first().authors.set(authors_id_list)
else:
book_obj = models.Book.objects.create(
**data
)
#添加这本书和作者的多对多关系
book_obj.authors.add(*authors_id_list)
return redirect('book_list')
-------------------------------------------
个性签名:代码过万,键盘敲烂!!!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!