Python-Web: Django Template Tags
if/else Tags The basic syntax format is as follows: {% if condition %} ... display {% endif %} Or: {% if condition1 %} ... display 1 {% elif condition2 %} ... display 2 {% else %} ... display 3 {% endif %} Output is determined based on the condition.
目录8 个章节
if/else Tags
The basic syntax format is as follows:
{% if condition %}
... display
{% endif %}
Or:
{% if condition1 %}
... display 1
{% elif condition2 %}
... display 2
{% else %}
... display 3
{% endif %}
Output is determined based on the condition. if/else supports nesting.
The {% if %} tag accepts the and, or, or not keywords to evaluate multiple variables, or to negate a variable (not), for example:
{% if athlete_list and coach_list %}
athletes 和 coaches 变量都是可用的。
{% endif %}
for Tag
{% for %} allows us to iterate over a sequence.
Similar to Python's for statement, the loop syntax is for X in Y, where Y is the sequence to iterate over and X is the variable name used in each particular loop.
In each loop, the template system renders all content between {% for %} and {% endfor %} .
For example, given an athlete_list variable which is a list of athletes, we can use the following code to display this list:
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
Adding a reversed to the tag makes the list iterate in reverse:
{% for athlete in athlete_list reversed %}
...
{% endfor %}
You can nest {% for %} tags:
{% for athlete in athlete_list %}
<h1>{{ athlete.name }}</h1>
<ul>
{% for sport in athlete.sports_played %}
<li>{{ sport }}</li>
{% endfor %}
</ul>
{% endfor %}
ifequal/ifnotequal Tags
The {% ifequal %} tag compares two values, and when they are equal, displays all the values between {% ifequal %} and {% endifequal %} .
The following example compares two template variables user and currentuser:
{% ifequal user currentuser %}
<h1>Welcome!</h1>
{% endifequal %}
Similar to {% if %}, {% ifequal %} supports an optional {% else%} tag:
{% ifequal section 'sitenews' %}
<h1>Site News</h1>
{% else %}
<h1>No News Here</h1>
{% endifequal %}
Comment Tags
Django comments use {# #}.
{# 这是一个注释 #}
Filters
Template filters can modify a variable before it is displayed. Filters use the pipe character, as shown below:
{{ name|lower }}
After the {{ name }} variable is processed by the lower filter, uppercase text is converted to lowercase.
Filter pipes can be * chained *, that is, the output of one filter pipe can in turn serve as the input of the next pipe:
{{ my_list|first|upper }}
The above example takes the first element and converts it to uppercase.
Some filters have arguments. Filter arguments follow a colon and are always enclosed in double quotes. For example:
{{ bio|truncatewords:"30" }}
This will display the first 30 words of the bio variable.
Other filters:
- addslashes : Adds a backslash before any backslash, single quote, or double quote.
- date : Formats a date or datetime object according to the specified format string argument. Example:
{{ pub_date|date:"F j, Y" }}
- length : Returns the length of the variable.
include Tag
The {% include %} tag allows including the contents of other templates in a template.
The following example includes the nav.html template:
{% include "nav.html" %}
About
To learn more interesting operations, please follow my WeChat official account: DealiAxy Every article is collected on my blog: blog.deali.cn
_EOF_
Python-Web: Django Template Tags
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Python-Web-Django:开始Django之旅
前言 开源的Web框架Django简单易用,稳定性和灵活性高,因此被广泛应用于商业化环境,它充分利用了Python拥有丰富的库这一优势。 Django的安装和服务器的搭建 安装Django库: pip install django 新建Web应用: django-admin startproject hello_django 以上命令会在当前文件夹下建立hello_django目录。 目录结构如下
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web-Django:第一个Web-App的构建
View视图渲染 编辑App目录下的views.py from django.shortcuts import render from django.http import HttpResponse # Create your views here. def hello(request): return HttpResponse(Hello Django!) URL解析配置 编辑urls
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web-Django:settings-py-配置
安装REST框架 pip install djangorestframework pip install django-rest-swagger 安装了REST Framework之后,之前创建的Django App(hello_app)就可以使用REST API进行通信。 我们所有创建的App都要添加到INSTALLED_APPS这个字段里面。 INSTALLED_APPS = [ #
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web:Django模板继承
模板可以用继承的方式来实现复用。 接下来我们先创建之前项目的 templates 目录中添加 base.html 文件,代码如下: !DOCTYPE html html head meta charset=utf-8 title标题/title /head body h1Hello World!/h1 pDA Django 测试。/p {% block mainbody %
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
讨论
共 0 条加载评论中…
