一、向数据库添加图书数据 【上接】https://blog.csdn.net/u010132177/article/details/103831173
1)首先开启mysql服务,并运行项目 1 2 3 4 5 启动mysql服务: net start mysql80 启动项目: py manage.py runserver
2)在templates/app1/book.html添加按钮 【1】添加新书按钮 <a href="/detail/{ {book.id} }">
hre里的斜杠/默认一定要加上,否则其它页面,如下面的delete中,会导致定向到 /index/delete/不存在 的页面中去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8" > <title>书籍页面</title> </head> <body> 本站的图书有:<br/> <ul> {% for book in books %} <!--通过href属性,把book的id做为参数传给详情页,进而查询对应英雄信息--> <li><a href="/detail/{{book.id}}"> {{book.btitle}}</a>:{{book.bpub_date}}</li> {%empty%} 暂时没有图书!!! {% endfor %} </ul> <!--【1】添加新书按钮--> <a href="/addInfo">添加一本三国书</a><br/> </body> </html>
3)app1/views.py 编写向数据表中增加书籍函数 【0】引入返回请求模块、重新定向模块 【0.1】引用时间模块 【1】添加书籍函数:创建bookinfo对象,并添加一本书 【2.0】成功 return HttpResponse('数据添加成功')
【2】添加成功后重新定向到页面:http://127.0.0.1:8000/books/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 from django.shortcuts import renderfrom app1.models import BookInfo from django.http import HttpResponse,HttpResponseRedirect from datetime import date def index (request) : '''app1应用:首页''' context={} context['hello' ]='hello world!!!' context['wa' ]='wawawawawahahahaha!' context['list' ]=list(range(1 ,10 )) return render(request,'app1/index.html' ,context) def books (request) : '''app1应用:图书列表页''' books=BookInfo.objects.all() return render(request,'app1/book.html' ,{'books' :books}) def detail (request,bookId) : '''app1应用:图书详情页,显示英雄信息''' book=BookInfo.objects.get(pk=bookId) heros=book.heroinfo_set.all() return render(request,'app1/detail.html' ,{'book' :book,'heros' :heros}) def addInfo (request) : '''添加新书到bookinfo表里''' b=BookInfo() b.btitle='水浒传' b.bpub_date=date(1989 ,9 ,9 ) b.save() return HttpResponseRedirect('/books' )
4)添加app1/urls.py信息 添加三国书
1 2 3 4 5 6 7 8 9 10 11 12 13 from django.urls import path,re_pathfrom . import viewsurlpatterns=[ path('app1/' ,views.index), path('books/' ,views.books), re_path(r"^detail/(\d+)" ,views.detail), path('addInfo/' ,views.addInfo), ]
点击后即向数据库添加一本书,添加成功后,重定向回books页面。
二、删除对应图书 1) templates/app1/book.html 关键行:<a href="/delete/{ {book.id} }">删除</a>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8" > <title>书籍页面</title> </head> <body> 本站的图书有:<br/> <ul> {% for book in books %} <!--通过href属性,把book的id做为参数传给详情页,进而查询对应英雄信息--> <li><a href="/detail/{{book.id}}"> {{book.btitle}}</a>:{{book.bpub_date}}-----<a href="/delete/{{book.id}}">删除</a> </li> {%empty%} 暂时没有图书!!! {% endfor %} </ul> <!--添加新书按钮--> <a href="/addInfo">添加一本书</a><br/> </body> </html>
2 )配置App1/urls.py 关键:path(r'delete/<int:bid>',views.deleteInfo), #删除对应图书
注意:path()间逗号一定别忘记写,否则可能导致未知错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from django.urls import path,re_pathfrom . import viewsurlpatterns=[ path('app1/' ,views.index), path('books/' ,views.books), re_path(r"^detail/(\d+)" ,views.detail), path('addInfo/' ,views.addInfo), path(r'delete/<int:bid>' ,views.deleteInfo), ]
3)app1/views.py编写删除函数 删除bookinfo id=bid的书籍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 from django.shortcuts import renderfrom app1.models import BookInfo from django.http import HttpResponse,HttpResponseRedirect from datetime import date def index (request) : '''app1应用:首页''' context={} context['hello' ]='hello world!!!' context['wa' ]='wawawawawahahahaha!' context['list' ]=list(range(1 ,10 )) return render(request,'app1/index.html' ,context) def books (request) : '''app1应用:图书列表页''' books=BookInfo.objects.all() return render(request,'app1/book.html' ,{'books' :books}) def detail (request,bookId) : '''app1应用:图书详情页,显示英雄信息''' book=BookInfo.objects.get(pk=bookId) heros=book.heroinfo_set.all() return render(request,'app1/detail.html' ,{'book' :book,'heros' :heros}) def addInfo (request) : '''添加新书到bookinfo表里''' b=BookInfo() b.btitle='水浒传' b.bpub_date=date(1989 ,9 ,9 ) b.save() return HttpResponseRedirect('/books' ) def deleteInfo (request,bid) : '''删除bookinfo id=bid的书籍''' b=BookInfo.objects.get(id=bid) b.delete() return HttpResponseRedirect('/books' )
点删除,删除对应图书,并重定向回books页面
三、重定向简写 关键:from django.shortcuts import render,redirect #引入重定向简写模块
使用:return redirect('/books') #【2】简写重定向
app1/views.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 from django.shortcuts import render,redirect from app1.models import BookInfo from django.http import HttpResponse,HttpResponseRedirect from datetime import date def index (request) : '''app1应用:首页''' context={} context['hello' ]='hello world!!!' context['wa' ]='wawawawawahahahaha!' context['list' ]=list(range(1 ,10 )) return render(request,'app1/index.html' ,context) def books (request) : '''app1应用:图书列表页''' books=BookInfo.objects.all() return render(request,'app1/book.html' ,{'books' :books}) def detail (request,bookId) : '''app1应用:图书详情页,显示英雄信息''' book=BookInfo.objects.get(pk=bookId) heros=book.heroinfo_set.all() return render(request,'app1/detail.html' ,{'book' :book,'heros' :heros}) def addInfo (request) : '''添加新书到bookinfo表里''' b=BookInfo() b.btitle='水浒传' b.bpub_date=date(1989 ,9 ,9 ) b.save() return HttpResponseRedirect('/books' ) def deleteInfo (request,bid) : '''删除bookinfo id=bid的书籍''' b=BookInfo.objects.get(id=bid) b.delete() return redirect('/books' )
效果同上