forloop.counter展示的是从1开始的有序数列for循环使用的
cmd创建一个库
create database 库名;
pycharm连接mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tsgl',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
'CHARSET': 'utf8'
}
}
模型层models创建表
from django.db import models
class Book(models.Model):
"""图书表"""
book_name = models.CharField(max_length=32, verbose_name='书名')
book_price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='价格')
publish_time = models.DateField(auto_now_add=True)
# 书籍与出版社的一对多关系字段
publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
# 书籍与作者的多对多的关系字段
authors = models.ManyToManyField(to='Author')
class Publish(models.Model):
"""出版社表"""
publish_name = models.CharField(max_length=32, verbose_name='出版社名称')
address = models.CharField(max_length=64, verbose_name='地址')
class Author(models.Model):
"""作者表"""
author_name = models.CharField(max_length=32, verbose_name='作者名字')
age = models.IntegerField(verbose_name='年龄')
# 作者表与作者详情表一对一的关系
author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)
class AuthorDetail(models.Model):
phone = models.BigIntegerField(verbose_name='电话')
address = models.CharField(max_length=32, verbose_name='家庭住址')
pycharm上往五个表中添加数据
首页的搭建
视图层views编写
访问这个地址返回一个html文件
def home_func(request):
return render(request, 'home.html')
路由层
path('home/', views.home_func, name='home_view'),
html文件编写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
{% load static %}
<link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css/">
<script src="/static/bootstrap-3.4.1-dist/js/bootstrap.min.js/"></script>
{% block css %}
{% endblock %}
</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="#">图书管理系统</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 class="active"><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="书名">
</div>
<button type="submit" class="btn btn-default">查询</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">jason</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-2">
<div class="list-group"> 绑定了路由跳转功能
<a href="{% url 'home_view' %}" class="list-group-item list-group-item-success">首页展示</a>
<a href="{% url 'book_list_view' %}" class="list-group-item list-group-item-info">出版社列表</a>
<a href="#" class="list-group-item list-group-item-warning">作者列表</a>
<a href="#" class="list-group-item list-group-item-danger">图书列表</a>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-default">
<div class="panel-heading">Panel heading without title</div>
<div class="panel-body">
Panel content
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">图书管理系统</h3>
</div>
<div class="panel-body">
{% block content %} 给子板修改的区域
<div class="page-header">
<h1>BMS<small>试试看吧</small></h1>
<div class="jumbotron">
<h1>最丰富的图书</h1>
<p>友情链接</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">点击就看</a></p>
</div>
<div class="row">
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://c-ssl.dtstatic.com/uploads/blog/202008/16/20200816111207_jmkqw.thumb.1000_0.jpg" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://c-ssl.dtstatic.com/uploads/blog/202008/16/20200816111207_jmkqw.thumb.1000_0.jpg" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://c-ssl.dtstatic.com/uploads/blog/202008/16/20200816111207_jmkqw.thumb.1000_0.jpg" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://c-ssl.dtstatic.com/uploads/blog/202008/16/20200816111207_jmkqw.thumb.1000_0.jpg" alt="...">
</a>
</div>
</div>
</div>
{% endblock %}
</div>
</div>
</div>
</div>
</div>
{% block js %}
{% endblock %}
</body>
</html>
展示数据功能
视图层编写
路由层
path('book_list/', views.book_list, name='book_list_view'),
from app01 import models 导入模型层使用orm查询数据
def book_list(request):
# 获取所有的图书
book_all = models.Book.objects.all()
# 返回html页面展示图书数据
return render(request, 'bookList.html', locals()) 接收数据
html文件编写
{% extends 'home.html' %} 继承母版
{% block content %} 内容的修改区域
<h2 class="text-center">图书展示页</h2>
<a href="{% url 'book_add_view' %}" class="btn btn-success">图书添加</a> 绑定事件
<table class="table table-striped table-hover">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>价格</th>
<th>日期</th>
<th>作者</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book_obj in book_all %} 拿出后端传过来的数据
<tr>
<td>{{ forloop.counter }}</td> <!--展示的是从1开始的有序数列-->
<td>{{ book_obj.book_name }}</td> <!--书名-->
<td>{{ book_obj.book_price }}</td> <!--价格-->
<td>{{ book_obj.publish_time|date:'Y-m-d' }}</td> <!--日期-->
{# <td>{{ book_obj.authors.all }}</td> <!--作者这里是对象-->#}
<td>
{% for author_obj in book_obj.authors.all %}
<span style="border: 3px solid #2aabd2;padding: 5px">{{ author_obj.author_name }}</span> 获取作者的名字,这是一个对象所以通过for循环拿到一个个作者名
第二种方法判断是不是最后一个作者不是加逗号
{% endfor %}
{# {% for author_obj in book_obj.authors.all %}#}
{# {% if forloop.last %}#}
{# {{ author_obj.author_name }}#}
{# {% else %}#}
{# {{ author_obj.author_name }},#}
{# {% endif %}#}
{# {% endfor %}#}
</td>
<td>{{ book_obj.publish.publish_name }}</td> <!--出版社-->
<td>
<a href="#" class="btn btn-primary btn-xs">编辑</a>
<a href="#" class="btn btn-danger btn-xs">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
书籍添加功能
视图层和路由层编写
path('book_add/', views.book_add_func, name='book_add_view')
def book_add_func(request):
接收前端的数据保存到数据库的表中
if request.method == 'POST':
name = request.POST.get('bool_name')
price = request.POST.get('book_price')
publish_time = request.POST.get('publish_time')
publish_id = request.POST.get('publish_id')
author_id_list = request.POST.getlist('author_id_list')
book_obj = models.Book.objects.create(book_name=name, book_price=price, publish_time=publish_time,
publish_id=publish_id)
book_obj.authors.add(*author_id_list) 把列表打散加入列表中跨表添加
return redirect('book_list_view') # 只能解析没有动态匹配的路由
# 把出版社和作者的数据都传给html文件
publish_all = models.Publish.objects.all()
author_all = models.Author.objects.all()
# 返回一个可以添加数据的html文件
return render(request, 'bookAdd.html', locals())
html文件编写
{% extends 'home.html' %} 继承母版
{% block content %}
<h2 class="text-center">图书添加页</h2>
<form action="" method="post"> 接收数据给后端
<p>book_name:
<input type="text" name="bool_name" class="form-control">
</p>
<p>book_price:
<input type="text" name="book_price" class="form-control">
</p>
<p>publish_time:
<input type="date" name="publish_time" class="form-control">
</p>
<p>publish_name:
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_all %}
<option value="{{ publish_obj.pk }}">{{ publish_obj.publish_name }}</option>
{% endfor %} pubilsh_id就是键,pubilsh_obj.pk就是值。publish_obj.publish_name展示所有的出版社选择通过主键知道是那个出版社
</select>
</p>
<p>author_list:
<select name="author_id_list" id="" multiple class="form-control"> multiple就是多选作者
{% for author_obj in author_all %}
<option value="{{ author_obj.pk }}">{{ author_obj.author_name }}</option>
{% endfor %}
</select>
</p>
<p>
<input type="submit" value="书籍添加" class="btn btn-block btn-success">
</p>
</form>
{% endblock %}
书本的修改
路由层和视图层
path('book_edit/<int:book_pk>/', views.book_edit_func, name='book_edit_view')
def book_edit_func(request, book_pk):
# 根据获取前端需要编辑的数据对象
edit_obj = models.Book.objects.filter(pk=book_pk).first() 拿到要修改的主键
if request.method == 'POST':
name = request.POST.get('bool_name')
price = request.POST.get('book_price')
publish_time = request.POST.get('publish_time')
publish_id = request.POST.get('publish_id')
author_id_list = request.POST.getlist('author_id_list')
models.Book.objects.filter(pk=book_pk).update(book_name=name, book_price=price, publish_time=publish_time,
publish_id=publish_id)
edit_obj.authors.set(author_id_list) 修改作者列表
return redirect('book_list_view')
# 获取出版社和作者的信息
publish_all = models.Publish.objects.all()
author_all = models.Author.objects.all()
# 传递给html页面展示
return render(request, 'bookEdit.html', locals())
html文件编写
{% extends 'home.html' %} 继承母版
{% block content %}
<h2 class="text-center">书籍编辑</h2>
<form action="" method="post">
<p>book_name:
<input type="text" name="bool_name" class="form-control" value="{{ edit_obj.book_name }}">
</p>
<p>book_price:
<input type="text" name="book_price" class="form-control" value="{{ edit_obj.book_price }}">
</p>
<p>publish_time:
<input type="date" name="publish_time" class="form-control" value="{{ edit_obj.publish_time|date:'Y-m-d' }}">
</p>
<p>publish_name:
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_all %}
根据主键点出版社在里面就默认选中不在就不选中
{% if edit_obj.publish == publish_obj %}
<option value="{{ publish_obj.pk }}" selected>{{ publish_obj.publish_name }}</option> selected默认选中
{% else %}
<option value="{{ publish_obj.pk }}" >{{ publish_obj.publish_name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<p>author_list:
<select name="author_id_list" id="" multiple class="form-control">
{% for author_obj in author_all %}
一样的判断通过主键点作者判断在不在这个列表里面在就选中
{% if author_obj in edit_obj.authors.all %}
<option value="{{ author_obj.pk }}"selected>{{ author_obj.author_name }}</option>
{% else %}
<option value="{{ author_obj.pk }}">{{ author_obj.author_name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<p>
<input type="submit" value="书籍添加" class="btn btn-block btn-danger">
</p>
</form>
{% endblock %}
删除功能
路由层和视图层
path('book_delete/<int:book_pk>/', views.book_delete_func, name='book_delete_view')
def book_delete_func(request, book_pk):
# 删除书籍与作者的绑定关系
delete_book_obj = models.Book.objects.filter(pk=book_pk).first()
delete_book_obj.authors.clear()
# 删除书籍数据
models.Book.objects.filter(pk=book_pk).delete()
return redirect('book_list_view')
html文件编写(基于书籍展示的编写)
<td>
<a href="{% url 'book_edit_view' book_obj.pk %}" class="btn btn-primary btn-xs">编辑</a>
<a href="{% url 'book_delete_view' book_obj.pk %}" class="btn btn-danger btn-xs delBtn">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
{% block js %}
<script>
$('.delBtn').click(function () {
let isDel = confirm('你确定要删除吗')
if (!isDel){
return false
}
})
</script>
{% endblock %}
标签:obj,name,管理系统,models,author,publish,book,图书
From: https://www.cnblogs.com/yinjinxi/p/17478959.html