1.找到app下的views.py文件,我的app文件名为book,写出bookinfos函数
from django.http import HttpResponse
from book.models import *
# Create your views here.
def index(request):
'''
:param request: 请求对象,请求的所有信息,都在这个对象里
:return:
'''
response = HttpResponse('request...%s'%request.method)
return response
'''以下为显示所有书籍信息函数'''
def bookinfos(request):
bookinfos = BookInfo.objects.all()
response = HttpResponse(bookinfos)
return response
2.在book文件下的urlpatterns中添加 url(r'^bookinfos$', views.bookinfos),
from django.conf.urls import include, url
from book import views
urlpatterns = [
url(r'^index$', views.index),
url(r'^bookinfos$', views.bookinfos), #添加
]

以上为没有配置模板,网页只能显示文字,无法美化页面
引用模板
1.定义模板




2.在book文件夹下views.py中添加bookinfos_list函数,其最下方函数111和上方注释的内容222等价
from django.http import HttpResponse
from book.models import *
from django.template import loader,RequestContext
from django.shortcuts import render
# Create your views here.
def index(request):
'''
:param request: 请求对象,请求的所有信息,都在这个对象里
:return:
'''
response = HttpResponse('request...%s'%request.method)
return response
'''
def bookinfos_list(request): #111
#查询所有书
bookinfos = BookInfo.objects.all()
#加载模板
template = loader.get_template('book/bookinfos_list.html')
#创建上下文对象,定义好参数,向模板传输参数
context = RequestContext(request,{'bookinfos':bookinfos})
#渲染模板
content = template.render(context)
#创建响应对象
response = HttpResponse(content)
#返回响应
return response
'''
def bookinfos_list(request): #2222
bookinfos = BookInfo.objects.all()
response = render(request=request,template_name='book/bookinfos_list.html',context={'bookinfos':bookinfos})
return response

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
bookinfo_list.html<hr/>
{{ bookinfos }}
<ul>
{% for bookinfo in bookinfos %}
<li>{{bookinfo.id}}-{{bookinfo.btitle}}</li>
{% endfor %}
</ul>
</body>
</html>

实现查询所有人物和详细信息
在book文件夹下urls.py下添加:
注意!这里正则表达式的分组,django自动将分组的值(group)传给后边的函数!
url(r'^heroinfo_detail/(\d+)$', views.heroinfos_detail),
在book文件夹下的views.py中添加:
hid就是分组的值的形参
def heroinfo_detail(request,hid):
return HttpResponse(hid)
页面效果测试如下


修改views.py
#根据人物id查询详细信息
def heroinfo_detail(request,hid):
#准备数据
heroinfo = HeroInfo.objects.get(id=hid)
#渲染数据并响应
response = render(request=request, template_name='book/heroinfo_detail.html', context={'heroinfo': heroinfo,'title':'详细信息'})
return response
创建heroinfo_detail页面:
<body>
<table>
<tr>
<td>名字:</td>
<td>{{ heroinfo.hname }}</td>
</tr>
<tr>
<td>性别:</td>
{% if heroinfo.hgender == True %}
<td>男</td>
{% else %}
<td>女</td>
{% endif %}
</tr>
<tr>
<td>简介:</td>
<td>{{heroinfo.hcontent}}</td>
</tr>
<tr>
<td>所属书的名字:</td>
<td>{{heroinfo.hbookinfo.btitle}}</td>
</tr>
</table>
</body>
创建heroinfos_list相关页面:
<body>
heroinfo_list.html<hr/>
<ul>
{% for heroinfo in heroinfos %}
<li><a href="/heroinfo_detail/{{ heroinfo.id}}">{{heroinfo.id}}-{{heroinfo.hname}}</a></li>
{% endfor %}
</ul>
</body>
增加urls.py下 heroinfos_list相关代码:
url(r'^heroinfos_list$', views.heroinfos_list),
增加views.py下 heroinfos_list相关代码:
def heroinfos_list(request):
heroinfos = HeroInfo.objects.all()
response = render(request=request,template_name='book/heroinfos_list.html',context={'heroinfos':heroinfos,'title':'显示所有英雄'})
return response
页面显示效果:

点击 至尊宝 跳转页面 并显示详细信息:

(不是重点)去除模板的硬编码:
在开发中,如果一些超链接都是硬编码,也就是说不是动态的,如果地址被改,链接网页将无法打开,例子如下:
html页面中的跳转链接:
<a href="/heroinfo_detail/{{ heroinfo.id}}">
book/urls.py文件下的匹配代码,中间添加111,使之无法匹配:
url(r'^heroinfo_111detail/(\d+)$', views.heroinfo_detail)

解决办法:
先在自己项目下的urls.py添加namespace,内容随便写,我这里是book:

url(r'^', include('book.urls',namespace='book'))
然后再book/urls.py下添加代码name:
url(r'^heroinfo_111detail/(\d+)$', views.heroinfo_detail,name='details')
最后在html页面中的跳转链接修改成如下代码:
<a href="{% url 'book:details' heroinfo.id %}">

网友评论