第八篇:Django之图书管理系统的数据增删改查
第八篇:Django之图书管理系统的数据增删改查
一、首页
我们简单创建一个网站,用来实现对图书数据的增删改查。网站的首页如下所示。
本网站的样式使用bootstrap框架进行搭建。
我们点击图书信息,进行页面跳转。
首页代码如下:
- urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 首页内容
url(r'^$', views.home, name='home'),
]
- views.py
# 首页
def home(request):
return render(request, 'home.html')
-
home.html
我们使用了模板的继承,使用
block
进行区域的划分。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>home</title>
{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap-3.4.1-dist/css/bootstrap.min.css' %}">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="{% static 'bootstrap-3.4.1-dist/js/bootstrap.min.js' %}"></script>
{#css#}
<style>
body {
background-color: wheat;
}
</style>
</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="{% url 'home' %}">图书管理系统</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><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="#">杨毅</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="{% url 'book_list' %}" 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">信息一览</h3>
</div>
<div class="panel-body">
{# 面板内部#}
{% block content %}
{#面板内容#}
<div class="jumbotron">
<h1>亚洲最大的图书系统</h1>
<p>我们不生产书,我们只是书的搬运工。</p>
<p>在这里,你可以找到你想要所有的书,并且完全免费,只有你想不到,没有你找不到的书。</p>
<p><a class="btn btn-primary btn-lg" href="" role="button">进入书籍页面</a></p>
</div>
{% endblock %}
</div>
</div>
</div>
</div>
</div>
{#js#}
{% block js %}
{% endblock %}
</body>
</html>
二、书籍展示页面
将mysql中的数据发往前端,并进行一系列操作。
- urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 首页内容
url(r'^$', views.home, name='home'),
# 书籍展示
url(r'^book/list/', views.book_list, name='book_list'),
]
- views.py
# 书籍展示页
def book_list(request):
# 拿到书籍表中的书籍对象
book_queryset = models.Book.objects.all()
return render(request, 'book_list.html', locals())
-
book_list.html
注意:关于注册时间,数据库中存储的时间需要进行模板语法的格式化才能展示到前端。
{## 继承首页页面#}
{% extends 'home.html' %}
{## 模板修改#}
{% block content %}
<h1 class="text-center">书籍列表</h1>
{#用来存放搜索栏和添加书籍按钮#}
<br>
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
<div class="col-lg-2 col-lg-offset-2">
<a href="{% url 'book_add' %}" class="btn btn-success">添加书籍</a>
</div>
</div>
<br>
<table class="table table-hover table-striped">
<thead>
<tr>
<th>序号</th>
<th>书籍</th>
<th>价格</th>
<th>出版社</th>
<th>出版时间</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book_obj in book_queryset %}
<tr>
<td>
{# 此处放序号,不用放逐渐#}
{{ forloop.counter }}
</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.price }}</td>
<td>{{ book_obj.publish.name }}</td>
{# 前端对数据库传来的数据进行模板语法转换#}
<td>{{ book_obj.publish_date|date:'Y-m-d' }}</td>
<td>
{# all不用加(),模板语法会自动进行识别#}
{% for author_obj in book_obj.author.all %}
{# 进行判断是不是最后一个#}
{% if forloop.last %}
{{ author_obj.name }}
{% else %}
{{ author_obj.name }},
{% endif %}
{% endfor %}
</td>
<td>
<a href="{% url 'book_edit' book_obj.pk %}" class="btn btn-warning btn-xs">编辑</a>
<a href="{% url 'book_delete' book_obj.pk %}" class="btn btn-danger btn-xs">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
三、添加书籍
我们点击添加书籍按钮,进行页面的跳转,如下所示。
相关代码如下;
- urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 首页内容
url(r'^$', views.home, name='home'),
# 书籍展示
url(r'^book/list/', views.book_list, name='book_list'),
# 添加书籍
url(r'^book/add/', views.book_add, name='book_add')
]
- views.py
# 书籍添加
def book_add(request):
if request.method == 'GET':
# 拿到出版社查询集【用来让用户进行选择出版社】
publish_queryset = models.Publish.objects.all()
# 拿到作者查询集【用来让用户进行选择作者】
author_queryset = models.Author.objects.all()
return render(request, 'book_add.html', locals())
elif request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
publish_date = request.POST.get('publish_date')
publish_id = request.POST.get('publish_id')
author_list = request.POST.getlist('author') # 多选,拿到列表类型
# 创建书籍对象【返回刚创建的对象】
book_obj = models.Book.objects.create(
title=title,
price=price,
publish_date=publish_date,
publish_id=publish_id
)
# 书籍 作者虚拟表创建关系
book_obj.author.add(*author_list) # 【*】打散
# 重定向
return redirect('book_list')
- book_add.html
{## 继承主页#}
{% extends 'home.html' %}
{% block content %}
<h1 class="text-center">添加书籍</h1>
<form action="" method="post">
<p>书籍:<input type="text" name="title" class="form-control"></p>
<p>价格:<input type="text" name="price" class="form-control"></p>
<p>出版时间:<input type="date" name="publish_date" class="form-control"></p>
<p>出版社:
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_queryset %}
{# 循环出版社对象,如果出版社对象等于书对应的出版社对象#}
<option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
{% endfor %}
</select>
</p>
<p>作者:
<select name="author" id="" class="form-control" multiple>
{% for author_obj in author_queryset %}
<option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
{% endfor %}
</select>
</p>
<input type="submit" class="btn btn-success btn-block" value="添加书籍">
</form>
{% endblock %}
四、编辑数据
我们点击编辑按钮,进行跳转到编辑页面。
- urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 首页内容
url(r'^$', views.home, name='home'),
# 书籍展示
url(r'^book/list/', views.book_list, name='book_list'),
# 添加书籍
url(r'^book/add/', views.book_add, name='book_add'),
# 编辑书籍
url(r'^book/edit/(\d+)', views.book_edit, name='book_edit')
]
- views.py
# 书籍编辑
def book_edit(request, book_id):
if request.method == 'GET':
# 根据书籍id拿到书籍对象
book_obj = models.Book.objects.filter(pk=book_id).first()
# 拿到出版社查询集【用来让用户进行选择出版社】
publish_queryset = models.Publish.objects.all()
# 拿到作者查询集【用来让用户进行选择作者】
author_queryset = models.Author.objects.all()
return render(request, 'book_edit.html', locals())
elif request.method == 'POST':
# 拿到post提交的数据
# print(request.POST)
# < QueryDict: {'title': ['《百年寂寞》'], 'price': ['45.80'], 'publish_date': ['2021-05-25'], 'publish': ['北京出版社'],'author': ['3']} >
title = request.POST.get('title')
price = request.POST.get('price')
publish_date = request.POST.get('publish_date')
publish_id = request.POST.get('publish_id')
author_list = request.POST.getlist('author') # 多选,拿到列表类型
# 修改数据库中的数据
# 修改书籍表中的数据
models.Book.objects.filter(pk=book_id).update(
title=title,
price=price,
publish_date=publish_date,
# 此处传入出版社对象即可
publish_id=publish_id
)
# 修改书籍表和作者表之间的对应关系
book_obj = models.Book.objects.filter(pk=book_id).first()
book_obj.author.set(author_list)
# 重定向
return redirect('book_list')
- book_edit.html
{## 继承主页#}
{% extends 'home.html' %}
{% block content %}
<h1 class="text-center">编辑数据</h1>
<form action="" method="post">
<p>书籍:<input type="text" name="title" value="{{ book_obj.title }}" class="form-control"></p>
<p>价格:<input type="text" name="price" value="{{ book_obj.price }}" class="form-control"></p>
<p>出版时间:<input type="date" name="publish_date" value="{{ book_obj.publish_date|date:'Y-m-d' }}" class="form-control"></p>
<p>出版社:
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_queryset %}
{# 循环出版社对象,如果出版社对象等于书对应的出版社对象#}
{% if publish_obj == book_obj.publish %}
<option value="{{ publish_obj.pk }}" selected>{{ publish_obj.name }}</option>
{% else %}
<option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<p>作者:
<select name="author" id="" multiple class="form-control">
{% for author_obj in author_queryset %}
{# 如果作者对象在书籍对应的作者对象里面#}
{% if author_obj in book_obj.author.all %}
{# 因为对应的第三章虚拟表,是通过主键进行添加的,所以传出pk即可#}
<option value="{{ author_obj.pk }}" selected>{{ author_obj.name }}</option>
{% else %}
<option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
编辑书籍:<input type="submit" class="btn btn-warning btn-block" value="编辑书籍">
</form>
{% endblock %}
五、删除书籍
我们点击删除按钮,书籍直接进行删除。【之后在进行优化(使用ajax进行二次确认)】
代码如下:
- urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 首页内容
url(r'^$', views.home, name='home'),
# 书籍展示
url(r'^book/list/', views.book_list, name='book_list'),
# 添加书籍
url(r'^book/add/', views.book_add, name='book_add'),
# 编辑书籍
url(r'^book/edit/(\d+)', views.book_edit, name='book_edit'),
# 删除数据
url(r'^book/delete/(\d+)', views.book_delete, name='book_delete'),
]
- views.py
# 书籍删除
def book_delete(request, book_id):
# 根据书籍id删除书籍对象
models.Book.objects.filter(pk=book_id).delete()
# 重定向
return redirect('book_list')