Django中的内置Tags
Dates
{% now "m/d/Y" %}
copyright {% now 'Y' as current_year %}
该tag也可以接受Django的date 变量,比如{% now 'SHORT_DATETIME_FORMAT' %}--> 09/05/2020 10:59 p.m.
{% now 'SHORT_DATE_FORMAT' %}-->09/05/2020
{% now 'DATE_FORMAT' %}-->Sept. 5, 2020
Forms
在
tag里使用{% csrf_token %}来防止跨域伪造请求攻击。
比较操作
{% if %}和{% elif %}和
与python 语法类似,不再赘述
{% first of %}是一个输出第一个不是空的变量的快捷用法。
#urls.py
{'var1':'','var2':1,'var3':2 }
{% firstof var1 var2 var3 %}
{% if %},{% elif %}的等效方法:
#firstof example
{% firstof var1 var2 var3 %} #Equivalent of firstof example
{%if var1 %}
{{ var1|safe }}
{% elif var2 %}
{{ var2|safe }}
{% elif var3 %}
{{ var3|safe }}
{% endif %} # Firstof example with a default value in case of no match( that is meaning all variables are empty)
{% firstof var1 var2 var3 'All vars are empty' %} #Assign the firstof result to another variable
{% firstof var1 var2 var3 as resultof %}
{% if in %}, {% if not in %}
{% if is value %}, {% if is not value %}
{% if value| %}
比如:
#urls.py
{'drinklist':['coco','cola','milk'],'userdrink':'cola'}
{% if drinklist|random == userdrink %}
Yes it is
{% else %}
No
{% endif %}
需要注意的是,在{% if %}里不允许有“()”,比如{% if drink in specials or drink== drink_of_day %},不能写成 {% if (drink in specials) or (drink== drink_of_day) %}
Loops
{% for %},以及带{% empty %}的
#urls.py
{'drinks':['cola','coco','water']}
#html
{% for drink in drinks %}
{{ drink }}
{% endfor %}
#urls.py
{'drinks':[]}
{% for drink in drinks %}
{{ drink }}
{% empty %}
No drinks here
{% endfor %}
{% for %}也产生了一系列变量来对遍历过程进行控制:
| Variable | Description |
|---|---|
| forloop.counter | The current iteration of the loop(1-indexed) |
| forloop.counter0 | The current iteration of the loop(0-indexed) |
| forloop.revcounter | The number of the iterations from the end of the loop(1-indexed) |
| forloop.revcounter0 | The number of the iterations from the end of the loop(0-indexed) |
| forloop.first | True,if it's the first time through the loop |
| forloop.last | True,if it's the last time through the loop |
#urls.py
'drinks':['water','cola','coco','milk']
#html
{% for drink in drinks %}
<b>{{ drink }}</b><br>
forloop.counter-->{{ forloop.counter }}<br>
forloop.counter0-->{{ forloop.counter0 }}<br>
forloop.revcounter0-->{{ forloop.revcounter0 }}<br>
forloop.revcounter-->{{ forloop.revcounter }}<br>
forloop.first-->{{ forloop.first }}<br>
forloop.last-->{{ forloop.last }}<br>
{% empty %}
No drinks here
{% endfor %}
(1)一般放在{% for %}内,循环时,依次取其参数,如:
#urls.py
'drinks':['water','cola','coco','milk','coffee','wanglaoji']
#base.html
<!DOCTYPE html>
<head>
<title>
{% block title_block %}
Base
{% endblock %}
</title>
<style>
.red {
color:red;
}
.blue {
color:blue;
}
.yellow {
color:yellow;
}
</style>
</head>
<body>
{% block body_block %}
{% endblock %}
</body>
#rendered.html
{% extends 'banners/base.html' %}
{% block title_block %}
test
{% endblock %} {% block body_block %}
<url>
{% for drink in drinks %}
<li class="{% cycle 'red' 'yellow' 'blue' %}">{{ drink }}</li>
</url>
{% endfor %}
{% endblock %}
drink在drinks中循环时,每次循环,依次取'red',‘yellow','blue',然后周而复始,如下:
{% cycle %} 的参数也可以是变量,如将上述urls.py和rendered.html换成如下,效果一样:
#urls.py
'drinks':['water','cola','coco','milk','coffee','wanglaoji'],'r':'red','b':'blue','y':'yellow'
#rendered.html
{% extends 'banners/base.html' %}
{% block title_block %}
test
{% endblock %}
{% block body_block %}
<url>
{% for drink in drinks %}
<li class="{% cycle r y b %}">{{ drink }}</li>
</url>
{% endfor %}
{% endblock %}
(2){% cycle arg1 arg2 .. as cc %}
{% cycle %}也不一定必须要在{% for %}内,也可以放在{% for %}外,在这种情况下,每次调用{% cycle arg1,arg2 %}都返回arg1,这样就达不到循环的目的了,怎么办?此时,正是{% cycle arg1 arg2 ... as cc %} 出现的原因了,as 将{% cycle arg1 arg2 ... %} "另存为" cc,之后的事情就比较有意思: 直接调用cc将只得到上次循环后的值,也就是说如果是“保存完" cc后,直接调用cc,将返回arg1,如果 {% cycle cc %}将”迫使“ cc进行循环,这时返回arg2,如果再直接调用cc呢,将返回arg2。代码的话,见下面:
# urls.py
'drinks':['water','cola','coco','milk','coffee','wanglaoji'],'r':'red','b':'blue','y':'yellow'
{% extends 'banners/base.html' %}
{% block title_block %}
test
{% endblock %}
{% block body_block %}
<url>
<li class="{% cycle r y b %}">test1</li>
<li class="{% cycle r y b %}">test4</li>
<li class="{% cycle r y b %}">test5</li>
</url>
<p>以上每次都调用'{% cycle r y b %} ' 都将是第一个参数,而不能依次取后面参数的值</p>
<url>
<li class="{% cycle r y b as testas %}">test1</li><span>("另存为testas")</span>
<li class="{{ testas }}">test2</li><span>(直接调用testas)</span>
<li class="{{ testas }}">test3</li><span>(直接调用testas)</span>
<li class="{% cycle testas %}">test4</li><span>("强迫循环")</span>
<li class="{{ testas }}">test5</li><span>(直接调用testas)</span>
<li class="{{ testas }}">test6</li><span>(直接调用testas)</span>
<li class="{% cycle testas %}">test7</li><span>("强迫循环")</span>
<li class="{{ testas }}">test8</li><span>(直接调用testas)</span>
</url>
{% endblock %}
在cc后添加 ”silent", 即{% cycle arg1 arg2 ... as cc silent %},然后再{% cycle cc %}时,将只是个“强迫”循环的声明,而不返回任何值,见下:
#rendered.html
{% extends 'banners/base.html' %}
{% block title_block %}
test
{% endblock %}
{% block body_block %}
<url>
<li class="{% cycle r y b %}">test1</li>
<li class="{% cycle r y b %}">test4</li>
<li class="{% cycle r y b %}">test5</li>
</url>
<p>以上每次都调用'{% cycle r y b %} ' 都将是第一个参数,而不能依次取后面参数的值</p>
<url>
<li class="{% cycle r y b as testas silent %}">test1</li><span>("仅声明循环")</span>
<li class="{{ testas }}">test2</li><span>(直接调用testas)</span>
<li class="{{ testas }}">test3</li><span>(直接调用testas)</span>
<li class="{% cycle testas %}">test4</li><span>("声明循环,不返回任何值")</span>
<li class="{{ testas }}">test5</li><span>(直接调用testas)</span>
<li class="{{ testas }}">test6</li><span>(直接调用testas)</span>
<li class="{% cycle testas %}">test7</li><span>("声明循环,不返回任何值")</span>
<li class="{{ testas }}">test8</li><span>(直接调用testas)</span>
</url>
{% endblock %}
重新设置循环,将循环值重新设定为第一个,如:
# rendered.html
{% extends 'banners/base.html' %}
{% block title_block %}
test
{% endblock %} {% block body_block %}
<url>
<li class="{% cycle r y b %}">test1</li> <li class="{% cycle r y b %}">test4</li>
<li class="{% cycle r y b %}">test5</li>
</url>
<p>以上每次都调用'{% cycle r y b %} ' 都将是第一个参数,而不能依次取后面参数的值</p>
<url>
<li class="{% cycle r y b as testas %}">test1</li><span>("仅声明循环")</span>
<li class="{{ testas }}">test2</li><span>(直接调用testas)</span>
<li class="{{ testas }}">test3</li><span>(直接调用testas)</span>
<li class="{% cycle testas %}">test4</li><span>("声明循环,不返回任何值")</span>
<li class="{{ testas }}">test5</li><span>(直接调用testas)</span> <li class="{{ testas }}">test6</li><span>(直接调用testas)</span>
{% resetcycle %}
<p>resetcycle,下次循环不再是蓝色,而将是第一个值也就是红色</p>
<li class="{% cycle testas %}">test7</li><span>("声明循环,不返回任何值")</span>
<li class="{{ testas }}">test8</li><span>(直接调用testas)</span>
</url>
{% endblock %}
regroup 有点像 pandas里的groupby ,即通过某个属性将一群数据进行分组,不同的是该tag分组后会自动生成两个属性:①grouper:item that was grouped ② list: a list of all items in this group
# urls.py
stores=[
{'name':'Downtown','street':'385 Main street','city':'San Diego'},
{'name':'Uptown','street':'231 Highland avenue','city':'San Deiego'},
{'name':'Midtown','street':'85 Balboa street','city':'Los Angeles'},
{'name':'Downtown','street':'639 Spring street','city':'Los Angeles'},
{'name':'Midtown','street':'1407 Broadway street','city':'Los Angeles'},
]
'stores':stores
# rendered.html
{% regroup stores by city as city_list %}
<ul>
{% for city in city_list %}
<li>{{ city.grouper }}</li>
<ul>
{% for item in city.list %}
<li>{{ item.name }}:{{ item.street }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
Python and Filter Operations
可将包在{% filter %}....{% endfilter %}的部分内容全部进行过滤。
#urls.py
'drinks':['cola','milk','water']
#rendered.html
{% filter upper %}
{% for drink in drinks %}
{{ drink }}
{% endfor %}
{% endfilter %}
对于创建没有被view方法或者必须通过“高昂”的操作才能得到的变量,用该tag就会非常方便。
#urls.py
'drink_cost':2
#rendered.html
{% with drinktax=drink_cost drinkpro=drink_cost %}
{{ drinktax}}<br>
{{drinkpro}}
{% endwith %}
Spacing and Special Characters
在{% autoescape on %}...{% endautoescape %}之间的将被转义:
# urls.py
'drink_cost':'<b>escape?</b>'
# rendered.html
{% autoescape on %}
{{ drink_cost }}
{% endautoescape %}
{% autoescape off %}
{{ drink_cost }}
{% endautoescape %}
{% spaceless %}...
仅仅移除tag之间的空隙。
用于输出组成template tag的特殊符号:
Argument Outputs openblock{%closeblock%}openvariable{{closevariable}}openbrace{closebrace}opencomment{#closecomment#}#rendered.html
{% templatetag openvariable %} url 'entry_list' {% templatetag closeblock %}
在{% verbatim %}...{% endverbatim %}之间的内容将被不被渲染。也可以在该tag中添加自定义的名字,即{% verbatim mytag %}...{% endverbatim %}
# rendered.html
{% verbatim myblock %}
Avoid template rendering via the {% verbatim %}{% endverbatim %} block.
{% endverbatim myblock %}
{% lorem [count] [method] [random] %}
三个可选参数:
Argument Description countA number (or variable) containing the number of paragraphs or words to generate (default is 1) methodEither wfor words,pfor HTML paragraphs orbfor plain-text paragraph blocks (default isb).randomThe word random, which if given, does not use the common paragraph (“Lorem ipsum dolor sit amet…”) when generating text.# rendered.html
{% lorem 3 p %}
Template structures
{% comment %}...{% endcomment %}之间的将不被渲染
# rendered.html
<p>Rendered text with </p>
{% comment %}
<p>Commented out text with {{ create_date|date:"c" }}</p>
{% endcomment %}
{ # #}:单独一行注释
<p>Great feeling</p>
{# This is comment #}
URLs
Django中的内置Tags的更多相关文章
- 在DevExpress程序中使用内置的图标构建美观的界面元素
在我们一般的程序中,为一般的界面元素添加一定的图标展示,有助于提升界面的整体的美观.结合排版布局,以及固定场景的图标,往往给用户非常好的直观感受:统一.美观.易理解.因此在一般的程序界面中,都尽量在略 ...
- Oracle中的内置函数在sql中的转换整理
程序里面经常会即支持Oracle数据库,又支持sql数据库.而有些Oracle内置函数用的比较多,但在sql中语法有些不同,我做了些整理,希望可以帮助大家.... 1.oracle中的内置函数:ora ...
- ThinkPHP中的内置标签
ThinkPHP中的内置标签 1.内置标签分类 闭合标签 <tag></tag> 开放标签 <tag /> 2.包含文件标签 主要功能:实现对文件的包含(类似于re ...
- C#使用Word中的内置对话框实例
本文实例讲述了C#使用Word中的内置对话框的方法,分享给大家供大家参考.具体实现方法如下: 使用 Microsoft Office Word 时,有时需要显示用户输入对话框.虽然可以创建自己的对话框 ...
- javascript中的内置对象
2015.12.1 javascript中的内置对象 复习: 1.js中的内置函数 alert prompt write parseInt parseFloat eval isNaN document ...
- Python常用模块中常用内置函数的具体介绍
Python作为计算机语言中常用的语言,它具有十分强大的功能,但是你知道Python常用模块I的内置模块中常用内置函数都包括哪些具体的函数吗?以下的文章就是对Python常用模块I的内置模块的常用内置 ...
- Thinkphp中的内置标签用法
Thinkphp中的内置标签有:Volist,Foreach,For,Switch,比较标签,范围判断标签,IF,Present,Empty,Defined,Assign,Define,标签嵌套,im ...
- JS中的内置对象简介与简单的属性方法
JS中的数组: 1.数组的概念: 数组是在内存中连续存储的多个有序元素的结构,元素的顺序称为下标,通过下标查找对应元素 2.数组的声明: ①通过字面量声明var arr1 = [,,,,] JS中同一 ...
- 秒懂ASP.NET中的内置对象
上篇博客,小编主要简单的介绍了一下ASP.NET中的控件,这篇博客,小编主要简单总结一下ASP.NET中的内置对象,七个内置对象分别是:Request.Response.Application.Coo ...
- jsp篇 之 Jsp中的内置对象和范围对象
Jsp中的内置对象: 在jsp页面代码中不需要声明,直接可以使用的对象. 一共有[9个内置对象]可以直接使用. 对象类型 名字 PageContext pageC ...
随机推荐
- OpenLayers change 事件获取当前值
这里有个 change:resolution 事件 但是事件的内容没有value,只有oldValue Zc {type: 'change:resolution', target: F, key: ' ...
- Easyexcel(2-文件读取)
同步读取 读取单个Sheet 通过sheet方法指定对应的Sheet名称或下标读取文件信息 通过doReadSync方法实现同步读取 @Data public class UserExcel { @E ...
- 【由技及道】量子跃迁部署术:docker+jenkins+Harbor+SSH的十一维交付矩阵【人工智障AI2077的开发日志011】
摘要: SSH密钥对构建的十一维安全通道 × Harbor镜像星门 × 错误吞噬者语法糖 = 在CI/CD的量子观测中实现熵减永动机,使容器在部署前保持开发与生产维度的叠加态 量子纠缠现状(技术背景) ...
- go kratos protobuf 接收动态JSON数据
前言 google.protobuf.Struct 是 Google Protocol Buffers 中的一种特殊类型,用于表示动态的键值对数据.它可以存储任意类型的数据,并提供了方便的方法来访问和 ...
- 人工智能-A*算法-最优路径搜索实验
上次学会了<A*算法-八数码问题>,初步了解了A*算法的原理,本次再用A*算法完成一个最优路径搜索实验. 一.实验内容1. 设计自己的启发式函数.2. 在网格地图中,设计部分障碍物.3. ...
- 自定义的 Vue 3 Composition API 钩子,antd标签
1. 创建自定义钩子 useDeviceStatus.js: import { computed } from 'vue'; export function useDeviceStatus(statu ...
- 跳转程序控制语句:break、continue 以及死循环、标号
1.break:结束循环,结束switch语句 . 案例:模拟用户登录密码,一共三次机会,初识密码为123456 我们之前学的方法可以完成这个案例,但是这种写法还存在问题 如图 明明已经输入了正确的密 ...
- 【JDBC第8章】数据库连接池
第8章:数据库连接池 8.1 JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接 进行sql操作 ...
- 请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序
Exception information: Exception type: MetadataException Exception message: 指定的架构无效. 错误: Model.LW.OT ...
- 探秘Transformer系列之(22)--- LoRA
探秘Transformer系列之(22)--- LoRA 目录 探秘Transformer系列之(22)--- LoRA 0x00 概述 0x01 背景知识 1.1 微调 1.2 PEFT 1.3 秩 ...