1.增加视图函数
#Http://127.0.0.1/books
def show_books(request):books = BookInfo.objects.all()return render(request,'booktest/show_books.html',{'books':books})
2.在booktest的视图模板文件夹下面新增show_books.html
图书信息如下:{%for book in books%}- {{book.btitle}}
{%endfor%}
3.修改app urls.py
from django.conf.urls import url
from . import viewsurlpatterns=[url(r'^index$',views.index),url(r'^index2$',views.index2),url(r'^books$',views.show_books),
]
4.访问Http://127.0.0.1/books
5.修改show_books视图模板
图书信息如下:
7.增加视图模板detail.html
8.增加视图函数
#Http://127.0.0.1/books/3
def detail(request,bid):book = BookInfo.objects.get(id=bid)heros = book.heroinfo_set.all()return render(request,'booktest/detail.html',{'book':book,'heros':heros})
9.修改视图模板detail.html
{{book.btitle}}
英雄信息如下:{% for hero in heros %}- {{hero.hname}}--{{hero.hcomment}}
{% empty %}- 没有英雄信息
{% endfor %}
10.修改app的urls.py
from django.conf.urls import url
from . import viewsurlpatterns=[url(r'^index$',views.index),url(r'^index2$',views.index2),url(r'^books$',views.show_books),url(r'^books/(\d+)$',views.detail),
]
11.访问Http://127.0.0.1:8000/books
12.点击某个图书,显示图书的名字以及相关的英雄信息