1. 【速查表单】
| 标签 | 描述 |
|---|---|
| 资源 | - |
| static | STATIC_ROOT 中的静态文件 |
| include | 加载指定模板 |
| block / extends | 继承和复写,参考 30. 模板继承 |
| url | 返回视图和可选参数匹配的URL(不带域名) |
| 模板内 | - |
| as | 存储在变量里 |
| with | 使用简单名字缓存一个复杂变量 |
| filter | 通过一个或多个过滤器对内容过滤 |
| 安全 | - |
| autoescape | 自动转义开关 |
| csrf_token | 为form表单提供csrf令牌 |
| 时间 | - |
| now | 显示当前的日期或时间 |
| comment | 可用于记录代码被注释掉的原因 |
| 其他 | - |
| for | 循环 |
| if | 判断 |
| cycle | 依次访问循环 |
| regroup | 重组 |
2. 资源
2.1. static
STATIC_ROOT 中的静态文件
{% load static %}<img src="{% static "images/hi.jpg" %}" alt="Hi" />
2.2. include
加载指定模板
{% include template_name %}
2.3. url
返回视图和可选参数匹配的URL(不带域名)
{% url 'some-url-name' v1 v2 %}
3. 模板内
3.1. as
存储在变量里
{% now "Y" as current_year %}{% blocktrans %}Copyright {{ current_year }}{% endblocktrans %}
3.2. with
使用简单名字缓存一个复杂变量
{% with total=business.employees.count %}{{ total }} employee{{ total|pluralize }}{% endwith %}# total 只在 with 内使用有效# 也可以分配多个变量,多个之间用空格
3.3. filter
通过一个或多个过滤器对内容过滤
{% filter force_escape|lower %}Lower TEXT ONLY{% endfilter %}
4. 安全
4.1. autoescape
自动转义开关
{% autoescape off %}Hello {{ name }}{% endautoescape %}
4.2. csrf_token
为form表单提供csrf令牌
{% csrf_token %}
5. 时间
5.1. now
显示当前的日期或时间
It is {% now "jS F Y H:i" %}
5.2. comment
可用于记录代码被注释掉的原因
{% comment "Optional note" %}<p>Commented out text with {{ create_date|date:"c" }}</p>{% endcomment %}
6. 其他
6.1. for 循环
6.1.1. 简单示例
{% for athlete in athlete_list %}<li>{{ athlete.name }}</li>{% endfor %}
6.1.2. Empty
for…empty
循环对象为空时的提示
{% for athlete in athlete_list %}<li>{{ athlete.name }}</li>{% empty %}<li>Sorry, no athletes</li>{% endfor %}
6.1.3. 循环中可用属性
| 属性 | 描述 |
|---|---|
| forloop.counter | 当前索引值,从1开始。常用于生成列表的序号 |
| forloop.counter0 | 当前索引值,从0开始 |
| forloop.revcounter | 循环结束的次数(从1开始) |
| forloop.revcounter0 | 循环结束的次数(从0开始) |
| forloop.first | 是否是循环的第一次,是为True。经常为第一行加点特殊,结合if用 |
| forloop.last | 最后一次循环,为真 |
| forloop.parentloop | 对于嵌套循环,返回父循环所在的循环次数。某些场景下,这是个大杀器,能解决很多头疼的问题。 |
6.2. if 判断
6.2.1. 基本示例
{% if athlete_list %}Number of athletes: {{ athlete_list|length }}{% elif athlete_in_locker_room_list %}Athletes should be out of the locker room soon{% else %}No athletes.{% endif %}
6.2.2. 使用过滤器和运算符
{% if messages|length >= 100 %}邮件太多等待处理{% endif %}{% if athlete_list|length > 1 %}组员:{% for athlete in athlete_list %} ... {% endfor %}{% else %}该球员:{{ athlete_list.0.name }}{% endif %}
6.2.3. 用 not / and / or 测试布尔值
优先级 and > or
{% if athlete_list and coach_list %}有 运动员 和 教练 时{% endif %}{% if not athlete_list %}没有运动员时{% endif %}{% if athlete_list or coach_list %}有 运动员 或 教练 时{% endif %}{% if not athlete_list or coach_list %}没有运动员 或 有教练 时{% endif %}{% if athlete_list and not coach_list %}有运动员 且 没有教练 时{% endif %}
6.2.4. 用 in / not in / is / is not
{% if "bc" in "abcdef" %}bc在abcdef里{% endif %}{% if "hello" in greetings %}greetings(列表、set)中有 hello{% endif %}{% if user not in users %}如果 users 是 QuerySet,当 user 不是 users 实例时{% endif %}{% if somevar is True %}somvar 是 True{% endif %}{% if somevar is not None %}somevar 不为 None{% endif %}
6.3. cycle
依次访问(第一次访问返回元素1,第二次访问返回参数2,以此类推)
6.3.1. 常规循环
加底色
{% for o in some_list %}<tr class="{% cycle 'row1' 'row2'%}">...</tr>{% endfor %}
输出
<tr class="row1">...</tr><tr class="row2">...</tr><tr class="row1">...</tr>
6.3.2. 控制循环
自己控制循环次数
<tr><td class="{% cycle 'row1' 'row2' as rowcolors %}">...</td><td class="{{ rowcolors }}">...</td></tr><tr><td class="{% cycle rowcolors %}">...</td><td class="{{ rowcolors }}">...</td></tr>
输出
<tr><td class="row1">...</td><td class="row1">...</td></tr><tr><td class="row2">...</td><td class="row2">...</td></tr>
6.4. regroup
用对象间共有的属性重组列表
比如有以下数据(字典):
cities = [{'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},{'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'},{'name': 'New York', 'population': '20,000,000', 'country': 'USA'},{'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'},{'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'},]
想要显示成:
- India
- Mumbai: 19,000,000
- Calcutta: 15,000,000
- USA
- New York: 20,000,000
- Chicago: 7,000,000
- Japan
- Tokyo: 33,000,000
就可以:
{% regroup cities by country as country_list %}<!-- cities 传入的列表 --><!-- country 用于分组的属性 --><!-- country_list 重组之后的列表名 --><ul>{% for country in country_list %}<li>{{ country.grouper }}# 分组的项目,比如字符串 India<ul>{% for city in country.list %}# .list 群组中所有项目列表<li>{{ city.name }}: {{ city.population }}</li>{% endfor %}</ul></li>{% endfor %}</ul>
Django 变量怎么传入到 JS 中
参考:
- Django与JS交互的示例代码-django js 获取 python 字典-Django 前后台的数据传递:https://www.cnblogs.com/chaoren399/p/11156175.html
HTML 中
<button onclick="open('{{ anyVariable }}')">Open</button>
Script 中
Django 传输时:
anyVariable = json.dumps(OriginalVariable)
在 Script 中
var anyName = '{{ anyVariable | safe }}';JSON.parse(anyName) // 即可正常使用
