day04 Django:ORM: 修改表字段 对数据(表记录)的sql语句操作:查询api,模糊查询 图书管理系统
day04 Django:ORM: 修改表字段 对数据(表记录)的sql语句操作:查询api,模糊查询 图书管理系统
一.Django: ORM模型层
1.修改表字段
在models.py里面直接改,之后执行那两条python语句
python manage.py makemigrations
python manage.py migrate
2.图书管理系统的请求流程
1.查看书籍
2.浏览器:
请求url http://localhost:8000/books/query/ 请求方式GET
2.django的urls.py
path('books/query/', views.booksquery, name="booksquery")
3.django的views.py
执行booksquery(request)
查询所有书籍,将查询的queryset数据传入模板,render()渲染出一个booksquery页面
4.templates的booksquery.html文件
编写要渲染的html
5.用户此时可以看到'查看书籍'的页面
3.对数据(表记录)的sql语句操作
3.1.增
3.2.删
3.3.改
3.4.查
3.4.1.查询api
rst = Book.objects.all() #查所有 #返回的是queryset
rst = Book.objects.filter(price=44, title='go',) #按条件查 #返回的是queryset
rst = Book.objects.exclude(price=44,) #按条件查的取反 #返回的是queryset
rst = Book.objects.get(title='java',) #查询的结果只能有一个,否则就报错 #返回的是model对象
rst = Book.objects.all().first() #取第一个 #返回的是model对象; 谁来调用: queryset可以调用
rst = Book.objects.all().last() #取最后一个 #返回的是model对象; 谁来调用: queryset可以调用
rst = Book.objects.all().order_by('pub_date',) #排序 #返回的是queryset; 谁来调用: queryset可以调用; 默认升序,降序用.order_by('-pub_date')
rst = Book.objects.all().order_by('nid').reverse() #翻转 #返回的是queryset; 谁来调用: queryset可以调用order_by('nid').reverse(); 等同于 order_by('-nid')
rst = Book.objects.all().count() #计数 #返回的是int; 谁来调用: queryset可以调用
rst = Book.objects.all().exists() #判断是否存在记录 #返回的是boolean; 谁来调用: queryset可以调用; 使用的是limit 1,效率高
rst = Book.objects.all().values('title', 'nid') #按字段取字典 #返回的是queryset; 谁来调用: queryset可以调用
rst = Book.objects.all().values_list('title',) #按字段取列表 #返回的是queryset; 谁来调用: queryset可以调用
rst = Book.objects.all().values('title').distinct() #去重 #返回的是queryset; 谁来调用: queryset的元组或字典来调用才有意义
3.4.2.模糊查询(双下划线)
rst = Book.objects.filter(price__gt=44) #大于
rst = Book.objects.filter(title__startswith='go') #以什么开头的
rst = Book.objects.filter(title__istartswith='go') #以什么开头的,不区分大小写
rst = Book.objects.filter(title__contains='g') #包含
rst = Book.objects.filter(title__icontains='go') #包含,不区分大小写
rst = Book.objects.filter(price__in=[100, 200, 300]) #在里面选一个
rst = Book.objects.filter(price__range=[100, 200]) #在范围里选一个
rst = Book.objects.filter(pub_date__year=2020, pub_date__month=2) #查日期: 没查到? 需要改配置 USE_TZ = False
4.图书管理系统
4.1.urls.py
增,删,改,查,首页 共5条线
from django.contrib import admin
from django.urls import path,re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('books/add/', views.booksadd, name="booksadd"),
path('books/query/', views.booksquery, name="booksquery"),
re_path('^$', views.booksquery, name="booksquery"),
re_path('books/delete/(\d+)', views.booksdelete, name="booksdelete"),
re_path('books/edit/(\d+)', views.booksedit, name="booksedit"),
]
4.2.app01/views.py
五条线的视图函数: 具体的逻辑,及展示对应的页面
from django.shortcuts import render, HttpResponse, redirect
from app01.models import Book
from django.urls import reverse
def booksadd(request):
if request.method == "POST":
# 方式一,较繁琐
# title = request.POST.get('title')
# price = request.POST.get('price')
# pub_date = request.POST.get('pub_date')
# publish = request.POST.get('publish')
# book = Book.objects.create(title=title, price=price, pub_date=pub_date, publish=publish)
# 方式二,注意form里的key要和表里的字段名一样
data = request.POST.dict()
del data["csrfmiddlewaretoken"]
book = Book.objects.create(**data)
return redirect(reverse('booksquery'))
else:
return render(request, 'booksadd.html', locals())
def booksquery(request):
book_list = Book.objects.all()
return render(request, 'booksquery.html', locals())
def booksdelete(request,delete_book_id):
Book.objects.filter(nid=delete_book_id).delete()
return redirect(reverse('booksquery'))
def booksedit(request, edit_book_id):
if request.method == "POST":
data = request.POST.dict()
del data["csrfmiddlewaretoken"]
book = Book.objects.filter(nid=edit_book_id).update(**data)
return redirect(reverse('booksquery'))
else:
edit_book = Book.objects.filter(nid=edit_book_id)[0]
return render(request, 'booksedit.html', locals())
4.3.templates
4.3.1.booksquery.html 查看书籍的页面
<!DOCTYPE html>
<html lang="zh-CN">
<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>Bootstrap 101 Template</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>查看书籍 !</h1>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<a href="{% url 'booksadd' %}" class="btn btn-primary btn-sm">添加书籍</a>
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>编号</th>
<th>书籍名称</th>
<th>价格</th>
<th>出版日期</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book in book_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book.title }}</td>
<td>{{ book.price }}</td>
<td>{{ book.pub_date|date:"Y-m-d" }}</td>
<td>{{ book.publish }}</td>
<td>
<a href="{% url 'booksedit' book.nid %}" class="btn btn-warning btn-sm">编辑</a>
<a href="{% url 'booksdelete' book.nid %}" class="btn btn-danger btn-sm">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
4.3.2.booksadd.html 增加书籍的页面
<!DOCTYPE html>
<html lang="zh-CN">
<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>Bootstrap 101 Template</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>添加书籍 !</h1>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form class="form-horizontal" action="{% url 'booksadd' %}" method="post">
{% csrf_token %}
<div class="form-group">
<label for="title" class="col-sm-2 control-label">书籍名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" placeholder="title" name="title">
</div>
</div>
<div class="form-group">
<label for="price" class="col-sm-2 control-label">价格</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="price" placeholder="price" name="price">
</div>
</div>
<div class="form-group">
<label for="pub_date" class="col-sm-2 control-label">出版日期</label>
<div class="col-sm-10">
<input type="date" class="form-control" id="pub_date" placeholder="pub_date" name="pub_date">
</div>
</div>
<div class="form-group">
<label for="publish" class="col-sm-2 control-label">出版社</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="publish" placeholder="publish" name="publish">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
4.3.3.booksedit.html 修改书籍的页面
<!DOCTYPE html>
<html lang="zh-CN">
<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>Bootstrap 101 Template</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>编辑书籍 !</h1>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form class="form-horizontal" action="{% url 'booksedit' edit_book.nid %}" method="post">
{% csrf_token %}
<div class="form-group">
<label for="title" class="col-sm-2 control-label">书籍名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" placeholder="title" name="title" value="{{ edit_book.title }}">
</div>
</div>
<div class="form-group">
<label for="price" class="col-sm-2 control-label">价格</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="price" placeholder="price" name="price" value="{{ edit_book.price }}">
</div>
</div>
<div class="form-group">
<label for="pub_date" class="col-sm-2 control-label">出版日期</label>
<div class="col-sm-10">
<input type="date" class="form-control" id="pub_date" placeholder="pub_date" name="pub_date" value="{{ edit_book.pub_date|date:'Y-m-d'}}">
</div>
</div>
<div class="form-group">
<label for="publish" class="col-sm-2 control-label">出版社</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="publish" placeholder="publish" name="publish" value="{{ edit_book.publish }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>