Django模板层

1. 模板变量

在 Django 模板中遍历复杂数据结构的关键是句点字符'.'

1.1 应用示例

views:

  1. def index(request): 
  2. # 字符串 
  3. s="hello" 
  4. # 列表 
  5. list=[111,222,333]  
  6. # 字典 
  7. dic={"name":"yuan","age":18}  
  8. # 日期对象 
  9. date = datetime.date(1993, 5, 2)  
  10.  
  11. class Person(object): 
  12. def __init__(self,name): 
  13. self.name=name 
  14. # 自定义类对象 
  15. person_yuan=Person("yuan")  
  16. person_egon=Person("egon"
  17. person_alex=Person("alex"
  18.  
  19. person_list=[person_yuan,person_egon,person_alex] 
  20.  
  21. return render(request,"index.html",{"s": s, list":list,"dic":dic,"date":date,"person_list":person_list}) 

template

  1. <h4>{{s}}</h4> 
  2. <h4>列表:{{ list.0 }}</h4> 
  3. <h4>列表:{{ list.2 }}</h4> 
  4. <h4>字典:{{ dic.name }}</h4> 
  5. <h4>日期:{{ date.year }}</h4> 
  6. <h4>类对象列表:{{ person_list.0.name }}</h4> 

注意: 句点符也可以用来引用对象的方法(无参数方法):
<h4>字典:{{ dic.name.upper }}</h4>

2. 模板过滤器

语法:

  1. {{obj|filter__name:param}} 

2.1 defualt

如果一个变量是false或者为空,使用给定的默认值。否则,使用变量的值。

  1. {{ value|default:"nothing" }} 

2.2 length

返回值的长度。它对字符串和列表都起作用。

  1. {{ value|length }} 
  2. ''' 
  3. value 是 ['a', 'b', 'c', 'd'],那么输出是 4 
  4. ''' 

2.3 filesizeformat

将值格式化为一个 “人类可读的” 文件尺寸 (例如 '13 KB', '4.1 MB', '102 bytes', 等等).

  1. {{ value|filesizeformat }} 
  2. ''' 
  3. value 是 123456789,输出将会是 117.7 MB 
  4. ''' 

2.4 date

  1. {{ value|date:"Y-m-d" }}  
  2. ''' 
  3. value=datetime.datetime.now() 
  4. ''' 

2.5 slice

  1. {{ value|slice:"2:-1" }} 
  2. ''' 
  3. value="hello world" 
  4. ''' 

2.6 truncatechars

字符串字符多于指定的字符数量,那么会被截断。截断的字符串将以可翻译的省略号序列(“...”)结尾。
参数:要截断的字符数

  1. {{ value|truncatechars:9 }} 

2.7 safe

Django的模板中会对HTML标签和JS等语法标签进行自动转义,关闭HTML的自动转义

  1. value="<a href="">点击</a>" 
  2. {{ value|safe}} 

3. 模板标签

3.1 for标签

  1. 遍历元素
  2. 遍历字典
  1. {% for key,val in dic.items %} 
  2. <p>{{ key }}:{{ val }}</p> 
  3. {% endfor %} 

注意: 循环序号可以通过{forloop}显示

  1. forloop.counter # The current iteration of the loop (1-indexed) 
  2. forloop.counter0 # The current iteration of the loop (0-indexed) 
  3. forloop.revcounter # The number of iterations from the end of the loop (1-indexed) 
  4. forloop.revcounter0 # The number of iterations from the end of the loop (0-indexed) 
  5. forloop.first # True if this is the first time through the loop 
  6. forloop.last # True if this is the last time through the loop 

3.2 for ... empty

for 标签带有一个可选的{% empty %} 从句,以便在给出的组是空的或者没有被找到时,可以有所操作

  1. {% for person in person_list %} 
  2. <p>{{ person.name }}</p> 
  3.  
  4. {% empty %} 
  5. <p>sorry,no person here</p> 
  6. {% endfor %} 

3.3 if 标签

{% if %}会对一个变量求值,如果它的值是“True”(存在、不为空、且不是boolean类型的false值),对应的内容块会输出。

  1. {% if num > 100 or num < 0 %} 
  2. <p>无效</p> 
  3. {% elif num > 80 and num < 100 %} 
  4. <p>优秀</p> 
  5. {% else %} 
  6. <p>凑活吧</p> 
  7. {% endif %} 

3.4 with

待整理

3.5 csrf_token

用于跨站请求伪造保护

4. 自定义标签和过滤器

待整理

5. 模板继承(extend)

5.1 应用示例

base.html:

  1. <!DOCTYPE html> 
  2. <html lang="en"> 
  3. <head> 
  4. <link rel="stylesheet" href="style.css" /> 
  5. <title>{% block title %} My amazing site {%/span> endblock %} </title> 
  6. </head> 
  7.  
  8. <body> 
  9. <div id="sidebar"> 
  10. {% block sidebar %} 
  11. <ul> 
  12. <li><a href="/">Home</a></li> 
  13. <li><a href="/blog/">Blog</a></li> 
  14. </ul> 
  15. {% endblock %} 
  16. </div> 
  17.  
  18. <div id="content"> 
  19. {% block content %} {% endblock %} 
  20. </div> 
  21. </body> 
  22. </html> 

子模板:

  1. {% extends "base.html" %} 
  2.  
  3. {% block title %}My amazing blog{% endblock %} 
  4.  
  5. {% block content %} 
  6. {% for entry in blog_entries %} 
  7. <h2>{{ entry.title }}</h2> 
  8. <p>{{ entry.body }}</p> 
  9. {% endfor %} 
  10. {% endblock %} 
  11.  
  12. <!--子模版的作用是用它们的内容填充空的blocks--> 
  • 在模版中使用 {% extends %} 标签,它必须是模版中的第一个标签。其他的任何情况下,模版继承都将无法工作。
  • 在base模版中设置越多的 {% block %} 标签越好。子模版不必定义全部父模版中的blocks
  • 从父模板({block)获取块的内容。{{block.super}}变量可以起到这个作用。添加到父块的内容而不是完全覆盖它.
  • 为了更好的可读性,可以给标签起一个名字,并且不能在一个模板中定义多个相同名字的标签
原文地址:https://www.cnblogs.com/sama/p/9301051.html