Tags

for

<ul>
{% for user in user_list %}
<li>{{ user.name }}</li>
{% endfor %}
</ul>

for循环可用的一些参数:

Variable Description
forloop.counter 当前循环的索引值(从1开始)
forloop.counter0 当前循环的索引值(从0开始)
forloop.revcounter 当前循环的倒序索引值(从1开始)
forloop.revcounter0 当前循环的倒序索引值(从0开始)
forloop.first 当前循环是不是第一次循环(布尔值)
forloop.last 当前循环是不是最后一次循环(布尔值)
forloop.parentloop 本层循环的外层循环

for ... empty

 
<ul>
{% for user in user_list %}
<li>{{ user.name }}</li>
{% empty %}
<li>空空如也</li>
{% endfor %}
</ul>
 

if,elif和else

 
{% if user_list %}
用户人数:{{ user_list|length }}
{% elif black_list %}
黑名单数:{{ black_list|length }}
{% else %}
没有用户
{% endif %}
 

当然也可以只有if和else

{% if user_list|length > 5 %}
七座豪华SUV
{% else %}
黄包车
{% endif %}

if语句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判断。

with

定义一个中间变量

{% with total=business.employees.count %}
{{ total }} employee{{ total|pluralize }}
{% endwith %}

csrf_token

这个标签用于跨站请求伪造保护。

在页面的form表单里面写上{% csrf_token %}

注释

{# ... #}

注意事项

1. Django的模板语言不支持连续判断,即不支持以下写法:

{% if a > b > c %}
...
{% endif %}

2. Django的模板语言中属性的优先级大于方法

def xx(request):
d = {"a": 1, "b": 2, "c": 3, "items": "100"}
return render(request, "xx.html", {"data": d})

如上,我们在使用render方法渲染一个页面的时候,传的字典d有一个key是items并且还有默认的 d.items() 方法,此时在模板语言中:

{{ data.items }}

默认会取d的items key的值。

母板

 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
{% block page-css %} {% endblock %}
</head>
<body> <h1>这是母板的标题</h1> {% block page-main %} {% endblock %}
<h1>母板底部内容</h1>
{% block page-js %} {% endblock %}
</body>
</html>
 

注意:我们通常会在母板中定义页面专用的CSS块和JS块,方便子页面替换。

继承母板

在子页面中在页面最上方使用下面的语法来继承母板。

{% extends 'layouts.html' %}

块(block)

通过在母板中使用{% block  xxx %}来定义"块"。

在子页面中通过定义母板中的block名来对应替换母板中相应的内容。

{% block page-main %}
<p>世情薄</p>
<p>人情恶</p>
<p>雨送黄昏花易落</p>
{% endblock %}

组件

可以将常用的页面内容如导航条,页尾信息等组件保存在单独的文件中,然后在需要使用的地方按如下语法导入即可。

{% include 'navbar.html' %}

静态文件相关

{% load static %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />

引用JS文件时使用:

{% load static %}
<script src="{% static "mytest.js" %}"></script>

某个文件多处被用到可以存为一个变量

{% load static %}
{% static "images/hi.jpg" as myphoto %}
<img src="{{ myphoto }}"></img>

使用get_static_prefix

{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!" />

或者

{% load static %}
{% get_static_prefix as STATIC_PREFIX %} <img src="{{ STATIC_PREFIX }}images/hi.jpg" alt="Hi!" />
<img src="{{ STATIC_PREFIX }}images/hi2.jpg" alt="Hello!" />

自定义simpletag

和自定义filter类似,只不过接收更灵活的参数。

定义注册simple tag

@register.simple_tag(name="plus")
def plus(a, b, c):
return "{} + {} + {}".format(a, b, c)

使用自定义simple tag

{% load app01_demo %}

{# simple tag #}
{% plus "1" "2" "abc" %}

inclusion_tag

多用于返回html代码片段

示例:

templatetags/my_inclusion.py

 
from django import template

register = template.Library()

@register.inclusion_tag('result.html')
def show_results(n):
n = 1 if n < 1 else int(n)
data = ["第{}项".format(i) for i in range(1, n+1)]
return {"data": data}
 

templates/result.html

<ul>
{% for choice in data %}
<li>{{ choice }}</li>
{% endfor %}
</ul>

templates/index.html

 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>inclusion_tag test</title>
</head>
<body> {% load my_inclusion %} {% show_results 10 %}
</body>
</html>
 
 
  

最新文章

  1. com.panie 项目开发随笔_功能任务设计(2016.12.28)
  2. docker搭建ros-indigo-arm交叉编译环境
  3. 点击显示div
  4. 基于mini2440的boa服务器移植
  5. 如何在子线程中使用Toast和更新UI
  6. SQL实现将一个表的数据插入到另外一个表的代码
  7. Mixing Delphi and C++(相互调用)
  8. Javascript继承实现
  9. DTCMS会员中心快速更改样式思路
  10. BZOJ2500: 幸福的道路
  11. 斯坦福NG机器学习课程:Anomaly Detection笔记
  12. python email ==&gt; send 发送邮件 :) [smtplib, email 模块]
  13. BZOJ 3564: [SHOI2014]信号增幅仪(随机增量法)
  14. vue-cli脚手架npm相关文件解读(1)webpack.base.conf.js
  15. 关于IE浏览器的一些思路
  16. 微服务(入门三):netcore ocelot api网关结合consul服务发现
  17. 第十四节:Lambda、linq、SQL的相爱相杀(3)
  18. 【Django】关于上传图片遇到的问题
  19. 第16月第8天 NSInvocation存储 函数指针 va_arg lldb
  20. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn&#39;t exist in the real number system ?)

热门文章

  1. [译]Understanding ECMAScript 6 说明
  2. SVN状态图标消失的解决方法
  3. iOS操作系统的层次结构
  4. CSS布局之-强大的负边距
  5. CSS的相对定位和绝对定位
  6. Educational Codeforces Round 11 _D
  7. Luogu P5349 幂
  8. spring-security中的csrf防御机制(跨域请求伪造)
  9. Electric Motor Manufacturer - Motor Protection: 5 Questions, 5 Answers
  10. jsTree展开根节点 设置用户图标