天天看點

django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結

  • 模闆是html頁面,可以根據視圖中傳遞的資料填充值
  • 建立模闆的目錄如下圖:
django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結
django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結
django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結
django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結
  • 修改settings.py檔案,設定TEMPLATES的DIRS值(目前目錄下的template目錄)
'DIRS': [os.path.join(BASE_DIR, 'templates')],
           
  • 在模闆中通路視圖傳遞的資料
{{輸出值,可以是變量,也可以是對象.屬性}}
{%執行代碼段%}
           

定義index.html模闆

<!DOCTYPE html>
<html>
<head>
  <title>首頁</title>
</head>
<body>
<h1>圖書清單</h1>
<ul>
{%for book in booklist%}
<li>
  <a href="{{book.id}}" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
    {{book.btitle}}
  </a>
</li>
{%endfor%}
</ul>
</body>
</html>
           

定義detail.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>
           

django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結

使用模闆

  • 編輯views.py檔案,在方法中調用模闆
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))
           

去除模闆的寫死

  • 在index.html模闆中,超連結是寫死的,此時的請求位址為“127.0.0.1/1/”
<a href="{{book.id}}" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
           
  • 看如下情況:将urlconf中詳細頁改為如下,連結就找不到了
url(r'^book/([0-9]+)/$', views.detail),
           
  • 此時的請求位址應該為“127.0.0.1/book/1/”
  • 問題總結:如果在模闆中位址寫死,将來urlconf修改後,位址将失效
  • 解決:使用命名的url設定超連結
  • 修改test1/urls.py檔案,在include中設定namespace
url(r'^admin/', include(admin.site.urls, namespace='booktest')),
           
  • 修改booktest/urls.py檔案,設定name
url(r'^book/([0-9]+)/$', views.detail, name="detail"),
           
  • 修改index.html模闆中的連結
<a href="{%url 'booktest:detail' book.id%}" target="_blank" rel="external nofollow" >
           

Render簡寫

  • Django提供了函數Render()簡化視圖調用模闆、構造上下文
django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結
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})
           

總結

  1. 安裝配置django運作的環境
  2. 編寫模型,使用簡單API與資料庫互動
  3. 使用django的背景管理中維護資料
  4. 通過視圖接收請求,通過模型擷取資料,展示出來
  5. 調用模闆完成展示
django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結
django-web開發架構-模闆-05使用模闆去除模闆的寫死Render簡寫總結