Django Template Language (DTL) 用于动态生成 HTML — 模板是包含特殊语法的 HTML 文件,用于插入数据、循环和条件判断。它故意限制了模板中的逻辑,将展示层与业务逻辑分离(业务逻辑应该在 views 中)。
三种主要语法
{{ article.title }}
Author: {{ article.author.name }}
{% if user.is_authenticated %}
Welcome, {{ user.username }}
{% else %}
Log in
{% endif %}
{% for article in articles %}
{{ article.title }}
{% empty %}
No articles yet.
{% endfor %}
{{ name|upper }}
{{ article.body|truncatewords:30 }}
{{ price|floatformat:2 }}
{{ date|date:"Y-m-d" }}
