标签:定义 请求 ret 执行 列表 mes klist oct 创建模板
‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)],
{{输出值,可以是变量,也可以是对象.属性}}
{%执行代码段%}
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>图书列表</h1>
<ul>
{%for book in booklist%}
<li>
<a href="{{book.id}}">
{{book.btitle}}
</a>
</li>
{%endfor%}
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>详细页</title>
</head>
<body>
<h1>{{book.btitle}}</h1>
<ul>
{%for hero in book.heroinfo_set.all%}
<li>{{hero.hname}}---{{hero.hcontent}}</li>
{%endfor%}
</ul>
</body>
</html>
from django.http import HttpResponse
from django.template import RequestContext, loader
from models import BookInfo
def index(request):
booklist = BookInfo.objects.all()
template = loader.get_template(‘booktest/index.html‘)
context = RequestContext(request, {‘booklist‘: booklist})
return HttpResponse(template.render(context))
def detail(reqeust, id):
book = BookInfo.objects.get(pk=id)
template = loader.get_template(‘booktest/detail.html‘)
context = RequestContext(reqeust, {‘book‘: book})
return HttpResponse(template.render(context))
<a href="{{book.id}}">
url(r‘^book/([0-9]+)/$‘, views.detail),
url(r‘^admin/‘, include(admin.site.urls, namespace=‘booktest‘)),
url(r‘^book/([0-9]+)/$‘, views.detail, name="detail"),
<a href="{%url ‘booktest:detail‘ book.id%}">
from django.shortcuts import render
from models import BookInfo
def index(reqeust):
booklist = BookInfo.objects.all()
return render(reqeust, ‘booktest/index.html‘, {‘booklist‘: booklist})
def detail(reqeust, id):
book = BookInfo.objects.get(pk=id)
return render(reqeust, ‘booktest/detail.html‘, {‘book‘: book})
标签:定义 请求 ret 执行 列表 mes klist oct 创建模板
原文地址:https://www.cnblogs.com/joshuazc/p/9533136.html