模板標籤

      在〈模板標籤〉中尚無留言

模板標籤

templates 目錄是存放 html 的地方,如果 html  裏的資料要進行變更,可以使用 Python 進行修改。

views.py 可以看成是主程式,”.html” 可以看成是函數。

views.py 檔要載入 html 檔,需使用 render 函數,第 2 個參數指定要載入的 html 檔,第 3 個參數為要傳入 html 的變數,必需使用字典{“變數名1”: 值1, “變數名2”: 值1, ….}

return render(request, 'test.html', 
{'currentTime': str(datetime.now()),'var_i':10, 'var_j':10}
)

模板簡介

底下是九九乘法表 table9.py 的代碼

🔒 底下內容僅限會員閱讀。

立即登入

模板標籤

{% xxx %} …. {% endxxx %} 其中的 “xxx” 稱為模板標籤 (template tags),這是為了在 html 中可以套用判斷式、迴圈及一般運算的關鍵字。

{{變數}} 標籤

在 html 中,可以使用 {{變數}} 接收 views.py 傳進來的變數,並列印在 html 中。

for 標籤

{% for %} …. {% endfor %} 可以讓 html 在這區塊中持續執行迴圈

if 標籤

{% if %}..{% else %}…{% endif %} 可以在這區塊中判定那些區塊要執行,那些不執行。請注意,

if var_now == i%,在 “==” 的前後都要空格。

🔒 底下內容僅限會員閱讀。

立即登入

widthratio 標籤

widthratio 也是一個標籤關鍵字,語法如下

 {% widthratio this_value max_value max_width %}

此語法的目的是為了求出如下的值

$(x=this\_value*\frac{max\_width}{max\_value})$

數字相乘

$(x=this\_value*\frac{max\_width}{1})$

其實只要記得第 2 個參數為 1 , 就是相乘

{% widthratio this_value 1 max_width %}

數字相除

$(x=this\_value*\frac{1}{max\_value})$

一樣只要記得第 3 個參數為 1 , 就是相除

{% widthratio this_value max_value 1 %}

todo

Template Filter

https://djangogirlstaipei.gitbooks.io/django-girls-taipei-tutorial/content/django/template_tags.html

todo

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *