from django.shortcuts import render,HttpResponse,redirect
from book.models import Book
from django.urls import reverse
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())
path = reverse("app01_book")
return redirect(path)
#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/")
def edit_book(request,edit_id):
if request.method =="GET":
edit_book = Book.objects.get(id=edit_id)
return render(request,"edit_book.html",{"edit_book":edit_book})
else:
data = request.POST.dict()
Book.objects.filter(id=edit_id).update(**data)
return redirect("/book/")