Django的模板系統

2022-05-10 03:37:24 字數 1058 閱讀 6687

1.乙個簡單的模板例子:

<

html

>

<

head

><

title

>ordering notice

title

>

head

>

<

body

>

<

h1>ordering notice

h1>

<

p>dear },

p>

<

p>

thanks for placing an order from }. it's scheduled to

ship on }.

p>

<

p>here are the items you've ordered:

p>

<

ul>

<

li>}

li>

ul>

<

p>your warranty information will be included in the packaging.

p>

<

p>

you didn't order a warranty, so you're on your own when

the products inevitably stop working.

p>

<

p>sincerely,<

br />}

p>

body

>

html

>

2.模板系統工作原理

3.模板的渲染

4.用render_to_response()簡化views層

簡化操作,不同載入模板那個過程render_to_response() 的第乙個引數必須是要使用的模板名稱。 如果要給定第二個引數,那麼該引數必須是為該模板建立 context 時所使用的字典。 如果不提供第二個引數, render_to_response() 使用乙個空字典。

Django模板系統

官方文件 只需要記兩種特殊符號 和 變數相關的用 邏輯相關的用。變數名由字母數字和下劃線組成。點 在模板語言中有特殊的含義,用來獲取物件的相應屬性值。幾個例子 view中 def template test request l 11,22,33 d class person object def i...

Django模板系統

如果在模板中需要解析變數使用 如果在模板中進行邏輯相關操作使用 注釋 單行注釋。在django的模板系統中變數使用的語法 當模版引擎遇到變數,就會用值替換掉變數。變數的命名可以是字母數字以及下劃線 的組合。變數名稱中不能有空格或標點符號。練習1 把html檔案中的 替換成 老王 html檔案 vie...

Django模板系統

匯入 template 和 context 類 from django.template import template,context def hello template request html 1.構建模板結構 template obj template html 2.建立渲染結構 cont...