返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
0_Python-Web-Django入门系列笔记

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.

曦远 (DealiAxy)约 9 分钟
目录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 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…