### 模板
* 模板是html頁面,可以根據視圖中傳遞的數據填充值
* 創建模板的目錄如下圖:

* 修改settings.py文件,設置TEMPLATES的DIRS值
~~~
'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}}">
{{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>
~~~
* * * * *
### 使用模板
* 編輯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}}">
~~~
* 看如下情況:將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%}">
~~~
* * * * *
### Render簡寫
Django提供了函數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.進程
- 1.1.fork
- 1.2.多個進程能否修改全局變量
- 1.3多次fork的問題
- 1.4.進程的創建-multiprocessing
- 1.5.進程的創建-Process子類
- 1.6.進程池Pool
- 1.7.進程間通信--Queue
- 2.線程
- 2.1.多線程-Threading
- 2.2.threading注意點
- 2.3.多線程-共享全局變量
- 2.4.線程和進程的對比
- 2.5.同步
- 2.6.互斥鎖
- 2.7.多線程-非共享數據
- 2.8.死鎖
- 2.9.同步應用
- 2.10.生產者與消費者模式
- 2.11.ThreadLocal
- 2.12.異步
- 2.13.GIL的問題
- 網絡編程
- 1.網絡概述-udp
- 1.1.TCP/IP
- 1.2.端口
- 1.3.ip地址
- 1.4.socket簡介
- 1.5.UDP介紹
- 1.6.udp網絡程序-發送數據
- 1.7.udp網絡程序-發送、接收數據
- 1.8.udp網絡程序-端口問題
- 1.9.udp綁定信息
- 2.0.udp網絡通信過程
- 2.1.udp應用:echo服務器
- 2.2.udp應用:聊天室
- 2.3.udp總結
- 2.4.udp綜合-模擬QQ
- 2.TFTP下載和上傳
- 3.TCP/IP
- 3.1.打開瀏覽器訪問百度的過程
- web服務器
- 1.1.MyWebServer.py
- 1.2.MyWebFramework.py
- 正則
- 1.1.re模塊
- 1.2.字符
- 1.3.原始字符串
- 1.4.表示數量
- 1.5.表示邊界
- 1.6.匹配分組
- 1.7.貪婪和非貪婪
- 數據結構和算法
- 1.引入概念
- 1.1.第一次嘗試
- 1.2.算法的提出
- 1.3.第二次嘗試
- 1.4.算法效率衡量
- 1.5.算法分析
- 1.6.常見時間復雜度
- 1.7.python內置類型性能分析
- 1.8.數據結構
- 2.順序表
- 2.1.順序表的形式
- 2.2.順序表的結構和實現
- 2.3.順序表的操作
- 2.4.python中的順序表
- 3.鏈表
- 3.1.單向鏈表
- 3.2.單向循環鏈表
- 3.3.雙向鏈表
- 4.棧
- 4.1.棧的結構實現
- 5.隊列
- 5.1.隊列的實現
- 5.2.雙端隊列
- 6.排序和搜索
- 6.1.冒泡排序
- 6.2.選擇排序
- 6.3.插入排序
- 6.4.快速排序
- 6.5.哈希排序
- 6.6.歸并排序
- 6.7.常見排序算法效率比較
- 6.8.搜索
- 7.樹與樹算法
- 7.1.二叉樹
- 7.2.二叉樹的遍歷
- 初識Django
- 1.小白
- 2.初次嘗試
- 3.管理站點
- 4.視圖
- 5.模板
- django模型
- 1.定義模型
- 2.模型成員
- 3.模型查詢
- 4.自連接
- django視圖
- django模板
- django高級
- django第三方
- django-git