Django框架图书管理之删除操作
作者:互联网
from django.shortcuts import render,HttpResponse
from book.models import Book
import datetime
# Create your views here.
def add_book(request):
if request.method == "GET":
return render(request, "add_book.html")
else:
#方式一
#title =request.POST.get("title")
#price = request.POST.get("price")
#pub_date = request.POST.get("pub_date")
#Book.objects.create(title=title,price=price,pub_date=pub_date)
#方式二
#request.POST.dict()
Book.objects.create(**request.POST.dict())
return redirect("/book/select")
#return HttpResponse("添加书籍")
#添加书籍
# 方式1
#date = datetime.date(year=2012,month=12,day=12)
#book = Book(title="xiyouji", price=199, pub_date=date)
book = Book(title="xiyouji",price=199,pub_date="2012-12-12")
print(book.id) #None
book.save() #执行sql
print(book.id) # 3
# 方式2
book = Book.objects.create(title="sanguoyanyi",price=299,pub_date="2011-11-11")
print(book.id)
print(book.price)
def select_book(request):
#return render(request,"add_book.html")
# (1) 查询所有的书籍,返回值是一个queryset类型对象
book_list= Book.objects.all()
'''
ORM数据引擎:
select id, title, price, pub_date from book:
+------+--------------+---------+------------+
id title price pub_date
+------+--------------+---------+------------+
1 西游记 499.00 2011-12-12
2 三国演义 399.00 2019-06-12
3 水浒传 299.00 2008-06-12
4 红楼梦 199.00 2020-06-12
+------+--------------+---------+------------+
book01 = Book(1 | 西游记 |499.00 |2011-12-12)
book02 = Book(2 | 三国演义 |399.00 |2019-06-12)
book03 = Book(3 | 水浒传 |299.00 |2008-06-12)
book04 = Book(4 | 红楼梦 |199.00 |2020-06-12)
book_list = queryset[book01,book02,book03,book04]
'''
#print ("book_list",book_list) # <QuerySet [<Book: Book object (1)>,...]
#QuerySet:支持索引操作,支持遍历
#book = book_list[0]
#print(book.id) # 1
#print(book.title) # 西游记
#print(book.price) # 499.00
# print(book_list.title)
#for book in book_list:
#print (book.id,book.title) #1 西游记 2 三国演义 3 水浒传 4 红楼梦
#return HttpResponse("添加书籍")
# (2) filter方法: 返回queryset
book_list = Book.objects.filter(price=455) #查看价格为455.00的书籍信息
# (3) exclude方法:排除符合条件的返回queryset类型对象
book_list = Book.objects.exclude(price=455) #排除价格等于455.00的书籍信息
# (4) get方法: 返回值模型类对象 特点: 查询有且只有一个对象时正常返回
book = Book.objects.get(id=7) #查看id为7的书籍信息(唯一一条)
print("book",book)
print("book", book.title,book.price) ##查看id为7的书籍信息的书籍名称和价格(唯一一条)
# (5) first方法,last方法
book = Book.objects.first() # 查询第一条书籍信息
book = Book.objects.last() # 查询最后一条书籍信息
print(book.title)
# (6) queryset.orderby方法:
book_list = Book.objects.all().order_by("-price") # 按价格排除
book_list = Book.objects.all().filter(price_gt=300).order_by("-price") #查询价格大于300.00,并排序
# (7) queryset.count() 计数:
n = book = Book.objects.count()
print (n) # 9
# (8) value(列表.字典),value_list(列表.元组)
#value(列表.字典)
book_list = Book.objects.values("title","price","pub_date")
book_list = Book.objects.filter(price=455).values("title","price","pub_date")
print("book_list", book_list)
#value_list(列表.元组)
book_list2 = Book.objects.filter(price=455).values("title","price","pub_date")
print("book_list2", book_list2)
return render(request,"books.html",{"book_list":book_list})
def update_book(request):
# 方式1:
book = Book.objects.get(pk=7) #更新id等于7的书籍名为菜根谭3
book.title = "菜根谭3"
print(book.title)
book.save() # updaate set
# 方式2:
Book.objects.filter(pk=7).update(title="菜根谭3") #更新id等于7的书籍名为菜根谭3
return HttpResponse("OK")
def delete_book(request,del_id):
if request.method == "GET":
return render(request,"delete_book.html")
# 方式1:
#book = Book.objects.get(pk=7) #删除id等于7的书籍信息
#book.delete()
# 方式2:
#book_list = Book.objects.filter(price=1199) #删除价格等于1199.00的书籍信息
#book_list.delete()
Book.objects.get(pk=del_id).delete()
return redirect("/book/select")
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path, include
from book.views import add_book,update_book,delete_book,select_book
urlpatterns = [
path('add_book', add_book),
path('select', select_book,name="app01_book"),
path('update', update_book),
re_path('delete/(\d+)', delete_book),
]
{% extends "base.html" %}
{% block content %}
<p class="jumbotron">您正在删除的书籍是 {{ book.title }} 价格为{{ book.price }}</p>
<form action="" method="post">
<input type="submit" value="确认删除?" class="btn danger">
</form>
{% endblock %}
{% extends "base.html" %}
{% block title %}
<title>书籍</title>
{% endblock %}
{% block content %}
<p>
<a href="/book/add_book" class="btn btn-primary">添加书籍</a>
</p>
<table>
<tr>
<th>序号</th>
<th>书籍名称</th>
<th>书籍价格</th>
<th>书籍出版日期</th>
<th>删除</th>
</tr>
{% 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><a class="del_btn" href="/book/delete/{{ book.id }}">删除</a></td>
</tr>
{% endfor %}
</table>
<script>
document.querySelectorAll(".del_btn");
for (var i=0;i<doms.length; i++) {
doms[i].onclick = function(){
var ret = confirm("确认删除?");
if(!ret){
return false
}
}
}
</script>
标签:删除,title,price,list,Django,Book,objects,book,图书 来源: https://www.cnblogs.com/A121/p/16487989.html