最终效果
新增
修改
背景
三张表: 【作者】 【书籍】 【出版社】
因为作者与书籍存在多对多所以会多生成出一张关系表
三个数据库字段
from django.db import models
# 作者表
class Author(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
age=models.IntegerField()
# 出版社表
class Publish(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
email = models.EmailField()
# 书籍表
class Book(models.Model):
nid = models.AutoField(primary_key=True)
title = models.CharField(max_length=32)
publishDate = models.DateField()
price = models.DecimalField(max_digits=5,decimal_places=2) # 最大长度5,2位小数
# 与 出版社建立1对多关系,外键在多的一方
publish = models.ForeignKey(to='Publish',to_field='id',on_delete=models.CASCADE)
# 与作者创建多对多关系
authors = models.ManyToManyField(to='Author')
视图
from django.shortcuts import render,redirect
from app01 import models
# 新增书籍
def add_book(request):
if request.method=='GET':
publish_list = models.Publish.objects.order_by('id')
author_list = models.Author.objects.order_by('id')
return render(request,'add_book.html',{'publish_list':publish_list,"author_list":author_list})
else:
title = request.POST.get('title')
price = request.POST.get('price')
pub_date = request.POST.get('pub_date')
publish_id = request.POST.get('publish_id')
authors_id_list = request.POST.getlist('authors_id_list') # 多选才这么操作!!getlist
book_obj = models.Book.objects.create(title=title,price=price,publishDate=pub_date,publish_id=publish_id)
book_obj.authors.set(authors_id_list)
return redirect('/')
# 书籍列表
def books(request):
books_list = models.Book.objects.all()
return render(request,'books.html',{'books_list':books_list})
# 修改书籍信息
def change_book(request,id):
if request.method == 'GET':
edit_obj = models.Book.objects.get(nid=id)
publish_list = models.Publish.objects.order_by('id')
author_list = models.Author.objects.order_by('id')
return render(request,'editbook.html',{'edit_obj':edit_obj,'publish_list':publish_list,'author_list':author_list})
else:
edit_id = request.POST.get('id')
title = request.POST.get('title')
price = request.POST.get('price')
pub_date = request.POST.get('pub_date')
publish_id = request.POST.get('publish_id')
authors_id_list = request.POST.getlist('authors_id_list')
edit_obj = models.Book.objects.get(nid=edit_id)
edit_obj.title = title
edit_obj.price = price
edit_obj.publishDate = pub_date
edit_obj.publish_id = publish_id
edit_obj.authors.set(authors_id_list)
# 或者方法2:
# models.Book.objects.get(nid=edit_id).update(title=title,price=price,....)
edit_obj.save()
return redirect('/')
# 删除书籍
def del_book(request,id):
del_obj = models.Book.objects.get(nid=id)
del_obj.delete()
return redirect('/')
HTML 采用Bootstrap
新增书籍
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
</head>
<body>
<h3>添加书籍</h3>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3"></div>
<form action="" method="post">
<div class="form-group"> <!--form-group 表单之间加间隙-->
<label for="">书名</label>
<input type="text" name="title" class="form-control"> <!--form-control 表单样式-->
</div>
<div class="form-group">
<label for="">价格</label>
<input type="text" name="price" class="form-control">
</div>
<div class="form-group">
<label for="">出版日期</label>
<input type="date" name="pub_date" class="form-control">
</div>
<div class="form-group">
<label for="">出版社</label>
<select name="publish_id" id="" class="form-control">
{% for publish in publish_list %}
<option value="{{ publish.id }}">{{ publish.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="">作者</label>
<select name="authors_id_list" id="" multiple class="form-control">
{% for author in author_list %}
<option value="{{ author.id }}">{{ author.name }}</option>
{% endfor %}
</select>
</div>
<div>
<input type="submit" class="btn btn-default">
</div>
</form>
</div>
</div>
</body>
</html>
打印列表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>书籍列表</title>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
</head>
<body>
<h3>书籍列表</h3>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>价格</th>
<th>出版社</th>
<th>出版日期</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for books in books_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ books.title }}</td>
<td>{{ books.price }}</td>
<td>{{ books.publish.name }}</td>
<td>{{ books.publishDate|date:'Y-m-d'}}</td>
<td>
{% for author in books.authors.all %}
{% if forloop.last %}
<span>{{ author.name }}</span>
{% else %}
<span>{{ author.name }}</span>,
{% endif %}
{% endfor %}
</td>
<td>
<a href="books/{{ books.nid }}/change" class="btn btn-warning">编辑</a>
<a href="books/{{ books.nid }}/del" class="btn btn-danger">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="/add_book/">
<input type="submit" value="新增书籍" class="btn btn-block">
</form>
</div>
</div>
</div>
</body>
</html>
修改书本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
</head>
<body>
<h3>添加书籍</h3>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3"></div>
<form action="" method="post">
<div class="form-group"> <!--form-group 表单之间加间隙-->
<label for="">书名</label>
<input type="text" name="title" class="form-control" value="{{ edit_obj.title }}"> <!--form-control 表单样式-->
</div>
<div class="form-group">
<label for="">价格</label>
<input type="text" name="price" class="form-control" value="{{ edit_obj.price }}">
</div>
<div class="form-group">
<label for="">出版日期</label>
<input type="date" name="pub_date" class="form-control" value="{{ edit_obj.publishDate|date:'Y-m-d'}}">
</div>
<div class="form-group">
<label for="">出版社</label>
<select name="publish_id" id="" class="form-control">
{% for publish in publish_list %}
{% if edit_obj.publish == publish %}
<option selected value="{{ publish.id }}">{{ publish.name }}</option> <!--默认选项加selected-->
{% else %}
<option value="{{ publish.id }}">{{ publish.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="">作者</label>
<select name="authors_id_list" id="" multiple class="form-control">
{% for author in author_list %}
{% if author in edit_obj.authors.all %}
<option selected value="{{ author.id }}">{{ author.name }}</option><!--默认选项加selected-->
{% else %}
<option value="{{ author.id }}">{{ author.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div style="display: none">
<input type="text" value="{{ edit_obj.nid }}" name="id">
</div>
<div>
<input type="submit" class="btn btn-default">
</div>
</form>
</div>
</div>
</body>
</html>
路由
from django.urls import path,re_path
from app01 import views
urlpatterns = [
re_path(r'^$',views.books),
path('add_book/', views.add_book),
re_path(r'books/(\d+)/change/$',views.change_book), # id 作为参数传递
re_path(r'books/(\d+)/del/$',views.del_book),
]