django建立專案案例1獲取並傳遞資料續04

阿啦卜發表於2018-07-18
  • 繼續django建立專案案例1續02的後續 繼續專案:

修改index.html程式碼如下:

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

image.png

修改views.py檔案程式碼如下:

#coding:utf-8
from django.shortcuts import render
from django.http import *
from .models import *
# from django.template import RequestContext,loader

# Create your views here.
def index(request):
    # temp = loader.get_template(`booktest/index.html`)
    # return HttpResponse(temp.render())
    bookList=BookInfo.objects.all()
    context={`list`:bookList}
    return render(request,`booktest/index.html`,context)

image.png

效果圖:
image.png


相關文章