项目路由控制器 app01.urls.py
from app01.views import *
urlpatterns = [
path("addbook/",book_add),
path("sqlinsert/",sql_insert),
path("search/",book_search),
re_path("edit/(\d+)/$",book_edit),
re_path("delete/(\d+)/$",book_delete)
]
项目视图控制器 app01.view.py
from app01 import models
def book_add(request):
if request.method=="POST":
#如果递交方式为post,获取新增数据的内容 有些内容是id
bk_name = request.POST.get("bk_name")
bk_price = request.POST.get("bk_price")
bk_pub_date = request.POST.get("bk_pub_date")
publish = request.POST.get("publish_id")
# getlist 是因为作者是多选项,我们如果用get只能获取一个值,但是应该获取的是一个列表
author = request.POST.getlist("author_lists")
#把数据都写入数据库
bk = models.Book.objects.create(title=bk_name,price=bk_price,pub_date=bk_pub_date,publish_id=publish)
bk.author.add(*author)
return redirect("/my/addbook/")
# return HttpResponse(bk_pub_date)
pubs = models.Publish.objects.all()
auths = models.Author.objects.all()
return render(request,"book_add.html",{"pubs":pubs,"auths":auths})
def sql_insert(request):
# 添加出版社数据
pub_lists = ["浙江出版社","山东出版社","济南出版社","南京出版社","北京出版社","天津出版社","深圳出版社"]
pub=[models.Publish.objects.create(name=i) for i in pub_lists]
# 添加作者详情表
auth_detail = models.Author_detail.objects.create(city="济南",add="济南区")
auth_detail = models.Author_detail.objects.create(city="山东",add="山东区")
auth_detail = models.Author_detail.objects.create(city="浙江",add="浙江区")
auth_detail = models.Author_detail.objects.create(city="南京",add="南京区")
auth_detail = models.Author_detail.objects.create(city="深圳",add="深圳区")
auth_detail = models.Author_detail.objects.create(city="天津",add="天津区")
auth_detail = models.Author_detail.objects.create(city="北京", add="北京区")
#添加作者详情,先根据城市获取作者详情表的id,再添加到作者表中
#让作者表中的auth_detail和详情ID产生关联
city_lists = ["浙江 ","山东","济南","南京","天津","深圳","北京"]
auth_name = ["alex","blex","clex","dlex","elex","flex","glex"]
for idx,city in enumerate(city_lists):
au_detail = models.Author_detail.objects.get(city=city)
auth1 = models.Author.objects.create(name=auth_name[idx],age=19+idx,author_detail=au_detail)
return HttpResponse("数据数据初始化添加完毕")
def book_search(request):
bks = models.Book.objects.all()
return render(request,"book_search.html",{"bks":bks})
def book_edit(request,idx):
bk = models.Book.objects.get(pk = idx)
publishs = models.Publish.objects.all()
auths = models.Author.objects.all()
if request.method=="POST":
bk_name = request.POST.get("bk_name")
bk_price = request.POST.get("bk_price")
bk_pub_date = request.POST.get("bk_pub_date")
publish = request.POST.get("publish")
auths = request.POST.getlist("author_lists")
models.Book.objects.filter(pk=idx).update(title=bk_name,price = bk_price,pub_date = bk_pub_date,publish_id = publish)
# bk.author.clear()
# bk.author.add(*auths)
# set等于上面2步,先清除对象的关闭,再新建新的关系表
bk.author.set(auths)
return redirect("/my/search/")
# author = bk.author.all()
#
# bk.update(title = bk_name)
# print(bk.pub_date)
return render(request,"book_edit.html",locals())
def book_delete(request,idx):
bk = models.Book.objects.filter(pk=idx)
bk.delete()
return redirect("/my/search")
项目ORM模块 appo1.models.py
from django.db import models
# Create your models here.
class Book(models.Model): # 意见
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=8, decimal_places=2)
pub_date = models.DateField()
publish = models.ForeignKey(to="Publish", to_field="pid", on_delete=models.CASCADE)
author = models.ManyToManyField(to="Author")
def __str__(self):
return self.title
class Publish(models.Model):
pid = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
class Author(models.Model):
aid = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
age = models.IntegerField()
author_detail = models.OneToOneField(to="Author_detail", to_field="aid", on_delete=models.CASCADE)
class Author_detail(models.Model): # 已建
aid = models.AutoField(primary_key=True)
add = models.CharField(max_length=32)
city = models.CharField(max_length=32)
网页book_add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book_add</title>
<link rel="stylesheet" href="/static/css/bootstrap.css">
</head>
<body>
<h2 class="text-center">书籍添加</h2>
<div class="container-fluid">
<div class="row">
<div class="col-md-4 col-lg-4 col-sm-3 col-md-offset-4 col-lg-offset-4 col-sm-offset-5">
<a href="/my/search" class="btn btn-success">返回查询页面</a>
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="add_book">书名</label>
<input type="text" name="bk_name" id="add_book" class="form-control">
</div>
<div class="form-group">
<label for="price">价格</label>
<input type="text" name="bk_price" id="price" class="form-control">
</div>
<div class="form-group">
<label for="pub_date">出版日期</label>
<input type="date" name="bk_pub_date" id="pub_date" class="form-control">
</div>
<div class="form-group">
<label for="pub_date">出版社</label>
<select name="publish_id" id="" class="form-control">
{% for pub in pubs %}
<option value="{{ pub.pk }}">{{ pub.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="author">作者</label>
<select name="author_lists" id="" class="form-control" multiple>
{% for auth in auths %}
<option value="{{ auth.pk }}">{{ auth.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group pull-right">
<input type="submit" class="btn btn-success" value="提交">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
网页book_edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book_add</title>
<link rel="stylesheet" href="/static/css/bootstrap.css">
</head>
<body>
<h2 class="text-center">书籍添加</h2>
<div class="container-fluid">
<div class="row">
<div class="col-md-4 col-lg-4 col-sm-3 col-md-offset-4 col-lg-offset-4 col-sm-offset-5">
<a href="/my/search" class="btn btn-success">返回查询页面</a>
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="add_book">书名</label>
<input type="text" name="bk_name" id="add_book" class="form-control">
</div>
<div class="form-group">
<label for="price">价格</label>
<input type="text" name="bk_price" id="price" class="form-control">
</div>
<div class="form-group">
<label for="pub_date">出版日期</label>
<input type="date" name="bk_pub_date" id="pub_date" class="form-control">
</div>
<div class="form-group">
<label for="pub_date">出版社</label>
<select name="publish_id" id="" class="form-control">
{% for pub in pubs %}
<option value="{{ pub.pk }}">{{ pub.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="author">作者</label>
<select name="author_lists" id="" class="form-control" multiple>
{% for auth in auths %}
<option value="{{ auth.pk }}">{{ auth.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group pull-right">
<input type="submit" class="btn btn-success" value="提交">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
网页book_search.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book_serach</title>
<link rel="stylesheet" href="/static/css/bootstrap.css">
</head>
<body>
<h2 class="text-center">书籍查询</h2>
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 col-md-5 col-sm-4 col-lg-offset-3 col-md-offset-3">
<a href="/my/addbook/" class="btn btn-primary">新增书籍</a>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th class="text-center">编码</th>
<th class="text-center">书籍名称</th>
<th class="text-center">出版日期</th>
<th class="text-center">价格</th>
<th class="text-center">出版社</th>
<th class="text-center">作者</th>
<th class="text-center">编辑操作</th>
<th class="text-center">删除操作</th>
</tr>
</thead>
<tbody>
{#bks是Book对象,里面的价格 书本名字可以直接通过属性获得,但是他的出版社需要通过正向查询的表名+字段获取 也就是bk.publiish.name#}
{% for bk in bks %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{bk.title}}</td>
<td>{{bk.pub_date|date:"Y-m-d"}}</td>
<td>{{bk.price}}</td>
<td>{{bk.publish.name}}</td>
<td>
{#先通过单个bk对象获取到author字段的所有数据 也就是bk.author.all 这里all不用加大括号 因为作者和书籍是多对多关系,所以bk.author.all 获取的不是一个值,而是多个值组成的queryset,还需要再次遍历提取作者表中的name字段#}
{% for each_bk_author_name in bk.author.all %}
{#这里实现的效果是作者和作者之间用逗号隔开,但是最后一个作者不需要加逗号,用forloop.last判断是否是最后一个循环,如果是不加逗号,否则模板语法后面加个逗号#}
{% if forloop.last %}
<span>{{each_bk_author_name.name}}</span>
{% else %}
<span>{{each_bk_author_name.name}},</span>
{% endif %}
{% endfor %}
</td>
<td><a href="/my/edit/{{ bk.pk }}/" class="btn btn-info">编辑操作</a></td>
<td><a href="/my/delete/{{ bk.pk }}/" class="btn btn-danger">删除操作</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>