此篇在Django小练习(2)的基础上增加对书籍的添加,删除,修改,查询功能,原理和练习2一致,主要是增加新的模板和视图方法。
1.创建模板
总共有3个页面
detail.html
<h2>Book List</h2>
<table>
<tr>
<td>书名</td>
<td>作者</td>
<td>出版社</td>
<td>出版日期</td>
</tr>
{% for book in book_list.all %}
<tr>
<td>{{book.name}}</td>
<td>{{book.author}}</td>
<td>{{book.pub_house}}</td>
<td>{{book.pub_date}}</td>
<td><a href="{% url 'lib:delBook' book.id %}">删除</a></td>
</tr>
{% endfor %}
</table>
<h4>查询</h4>
<form action="{% url 'lib:searchBook' %}" method="post" name="searchBook">
{% csrf_token %}
<p><span>书名:</span><input type="text" name="name">
<input type="submit" value="查询" ></p>
</form>
<h4>添加</h4>
<form action="{% url 'lib:addBook' %}" method="post" name="addBook">
{% csrf_token %}
<p><span>书名:</span><input type="text" name="name"></p>
<p><span>作者:</span><input type="text" name="author"></p>
<p><span>出版社:</span><input type="text" name="pub_house">
<input type="submit" value="提交"></p>
</form>
<h4>编辑</h4>
<form action="{% url 'lib:editBook' %}" method="post" name="editBook">
{% csrf_token %}
<p><span>书名:</span><input type="text" name="name"></p>
<p><span>作者:</span><input type="text" name="author"></p>
<p><span>出版社:</span><input type="text" name="pub_house">
<input type="submit" value="提交"></p>
</form>
notFound.html
<h1>Book List Empty</h1>
search.html
<h1>查询结果</h1>
<table>
<tr>
<td>书名</td>
<td>作者</td>
<td>出版社</td>
<td>出版日期</td>
</tr>
{% for book in book_list.all %}
<tr>
<td>{{book.name}}</td>
<td>{{book.author}}</td>
<td>{{book.pub_house}}</td>
<td>{{book.pub_date}}</td>
</tr>
{% endfor %}
</table>
2.创建视图
#V 视图,负责业务逻辑,并在适当时候调用模型model和模板Template,myblog/lib/views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Book
from django.http import HttpResponseRedirect
from django.urls import reverse
def index(request):
return HttpResponse("Hello,world!\n Welcome to django!")
def detail(request):
book_list=Book.objects.order_by('-pub_date')[:5]#展示最新的5条记录
context={'book_list':book_list}
return render(request,'lib/detail.html',context)
def addBook(request):
if request.method=='POST':
temp_name=request.POST['name']
temp_author=request.POST['author']
temp_pub_house=request.POST['pub_house']
from django.utils import timezone
temp_book=Book(name=temp_name,author=temp_author,pub_house=temp_pub_house,pub_date=timezone.now())
temp_book.save()
return HttpResponseRedirect(reverse('lib:detail'))
def deleteBook(request,book_id):
bookID=book_id
Book.objects.filter(id=bookID).delete()
return HttpResponseRedirect(reverse('lib:detail'))
def searchBook(request):
if request.method=='POST':
bookName=request.POST['name']
book_list=Book.objects.filter(name__icontains=bookName)#名字中包含bookName并不区分大小写
if book_list:
context={'book_list':book_list}
return render(request,'lib/search.html',context)
else:
return render(request,'lib/notFound.html')
def editBook(request):
if request.method=='POST':
temp_name=request.POST['name']
temp_author=request.POST['author']
temp_pub_house=request.POST['pub_house']
twz = Book.objects.get(name=temp_name)
print(twz)
if twz:
twz.author=temp_author
twz.pub_house=temp_pub_house
twz.save()
return HttpResponseRedirect(reverse('lib:detail'))
else:
return render(request,'lib/notFound.html')
3.绑定链接
#URL分发器的作用是将页面请求分发给不同的视图(View)处理,视图再调用相应的模型(Model)和模板(Template)。myblog/myblog/urls.py
from django.urls import path
from . import views
app_name='lib'
urlpatterns=[
path('',views.index,name='index'),
path('detail/',views.detail,name='detail'),
path('addBook/',views.addBook,name='addBook'),
path('delBook/<int:book_id>',views.deleteBook,name='delBook'),
path('searchBook/',views.searchBook,name='searchBook'),
path('editBook/',views.editBook,name='editBook'),
]
4.运行项目
打开网页:http://127.0.0.1:8000/lib/detail/,显示如下: