Django框架 路由的反向解析(别名)
![](https://img2022.cnblogs.com/blog/1413093/202207/1413093-20220723180128581-1968572682.png)
"""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', add_book,name="addBook"),
path('select', select_book,name="app01_book"),
path('update', update_book),
re_path('delete/(\d+)', delete_book),
]
![](https://img2022.cnblogs.com/blog/1413093/202207/1413093-20220723180146600-984540812.png)
{% extends "base.html" %}
{% block title %}
<title>书籍</title>
{% endblock %}
{% block content %}
<p>
<a href="{% url 'addBook' %}" 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>
![](https://img2022.cnblogs.com/blog/1413093/202207/1413093-20220723180210509-620453989.png)
{% extends "base.html" %}
{% block title %}
<form action="{% url 'addBook' %}" method="post">
<div class="from-group">
<lable for="">书籍名称</lable>
<input type="text" class="form-control">
</div>
<div class="from-group">
<lable for="">书籍价格</lable>
<input type="text" class="form-control">
</div>
<div class="from-group">
<lable for="">出版日期</lable>
<input type="date" class="form-control">
</div>
<div class="from-group">
<input type="submit" class="btn" success pull-right>
</div>
</form>
{% endfor %}
![](https://img2022.cnblogs.com/blog/1413093/202207/1413093-20220723180929345-779833480.png)
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/select")