django模板原理html
# 建立template對象,由context對象傳遞template所須要的值, 有render方法進行模板的呈現python
# 寫模板,建立 Template 對象,建立 Context , 調用 render() 方法。git
# Python 字符串都有 upper() 和 isdigit() 方法,你在模板中調用django
# 執行變量 {{ abc }} # 判斷 {% if x > 0 %} {% else %} {% endif %} 例: {% if today_is_weekend %} <p>Welcome to the weekend!</p> {% endif %} 注意: 模板中不能包括 () {% if athlete_list or coach_list %} There are some athletes or some coaches. {% endif %} # 循環 {% for i in items_list %} 例: {% for athlete in athlete_list reversed %} <li>{{ athlete.name }}</li> {% empty %} <p>There are no athletes. Only computer programmers.</p> {% endfor %} # forloop的一個用法 # forloop循環的結構控制語法 {% for link in links %}{{ link }}{% if not forloop.last %} | {% endif %}{% endfor %} # forloop實現結構控制的變量 forloop.parentloop.counter 父計數器 # forloop.counter 當前計數器 接下來能夠採用 if 進行邏輯控制 {% for country in countries %} <table> {% for city in country.city_list %} <tr> <td>Country #{{ forloop.parentloop.counter }}</td> <td>City #{{ forloop.counter }}</td> <td>{{ city }}</td> </tr> {% endfor %} </table> {% endfor %} # 比較兩個變量的值 {% ifequal section 'sitenews' %} <h1>Site News</h1> {% else %} <h1>No News Here</h1> {% endifequal %} #註釋及多行註釋 {# This is a comment #} {% comment %} This is a multi-line comment. {% endcomment %} # 過濾器以後管道給lower ,,, truncatewords {{ name | lower }} {{ pub_date | date:"F j, Y" }} # 模板加載 import os.path TEMPLATE_DIRS = ( os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'), ) from django.shortcuts import render_to_response return render_to_response('current_datetime.html', {'current_date': now}) # 模板的繼承 base.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en"> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <h1>My helpful timestamp site</h1> {% block content %}{% endblock %} {% block footer %} <hr> <p>Thanks for visiting my site.</p> {% endblock %} </body> </html> {% extends "base.html" %} {% block title %}The current time{% endblock %} {% block content %} <p>It is now {{ current_date }}.</p> {% endblock %}