Django 模板语法

BBigSun 1 289阅读模式

标签

if/else

{% if today_is_weekend %}
    <p>Welcome to the weekend!</p>
{% endif %}
{% if today_is_weekend %}
    <p>Welcome to the weekend!</p>
{% else %}
    <p>Get back to work.</p>
{% endif %}

嵌套:

{% if athlete_list %}
    <p>Here are the athletes: {{ athlete_list }}.</p>
{% else %}
    <p>No athletes are available.</p>
    {% if coach_list %}
        <p>Here are the coaches: {{ coach_list }}.</p>
    {% endif %}
{% endif %}

for

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% endfor %}
</ul>

反向迭代:文章源自十年又十年-https://www.bbigsun.com/464.html

{% for athlete in athlete_list reversed %}
...
{% endfor %}

嵌套:文章源自十年又十年-https://www.bbigsun.com/464.html

{% for athlete in athlete_list %}
    <h1>{{ athlete.name }}</h1>
    <ul>
    {% for sport in athlete.sports_played %}
        <li>{{ sport }}</li>
    {% endfor %}
    </ul>
{% endfor %}

在执行循环之前先检测列表的大小是一个通常的做法,当列表为空时输出一些特别的提示。文章源自十年又十年-https://www.bbigsun.com/464.html

{% if athlete_list %}
    {% for athlete in athlete_list %}
        <p>{{ athlete.name }}</p>
    {% endfor %}
{% else %}
    <p>There are no athletes. Only computer programmers.</p>
{% endif %}

等价于:文章源自十年又十年-https://www.bbigsun.com/464.html

{% for athlete in athlete_list %}
    <p>{{ athlete.name }}</p>
{% empty %}
    <p>There are no athletes. Only computer programmers.</p>
{% endfor %}

在每个{% for %}循环里有一个称为forloop 的模板变量。这个变量有一些提示循环进度信息的属性。文章源自十年又十年-https://www.bbigsun.com/464.html

  • forloop.counter 总是一个表示当前循环的执行次数的整数计数器。 这个计数器是从1开始的,所以在第一次循环时 forloop.counter 将会被设置为1。
  • forloop.counter0 类似于 forloop.counter ,但是它是从0计数的。 第一次执行循环时这个变量会被设置为0。
  • forloop.revcounter 是表示循环中剩余项的整型变量。 在循环初次执行时 forloop.revcounter 将被设置为序列中项的总数。 最后一次循环执行中,这个变量将被置1。
  • forloop.revcounter0 类似于 forloop.revcounter ,但它以0做为结束索引。 在第一次执行循环时,该变量会被置为序列的项的个数减1。
  • forloop.first 是一个布尔值,如果该迭代是第一次执行,那么它被置为````
  • forloop.last 是一个布尔值;在最后一次执行循环时被置为True。 一个常见的用法是在一系列的链接之间放置管道符(|)
  • forloop.parentloop 是一个指向当前循环的上一级循环的 forloop 对象的引用(在嵌套循环的情况下)。
{% for object in objects %}
    {% if forloop.first %}<li class="first">{% else %}<li>{% endif %}
    {{ object }}
    </li>
{% endfor %}

{% for link in links %}{{ link }}{% if not forloop.last %} | {% endif %}{% endfor %}

{% for country in countries %}
    <table>
    {% for city in country.city_list %}
        <tr>
        <td>Country #{{ forloop.parentloop.counter }}</td>
        <td>City #{{ forloop.counter }}</td>
        <td>{{ city }}</td>
        </tr>
    {% endfor %}
    </table>
{% endfor %}

ifequal

{% ifequal %} 标签比较两个值,当他们相等时,显示在 {% ifequal %} 和 {% endifequal %} 之中所有的值。文章源自十年又十年-https://www.bbigsun.com/464.html

{% ifequal user currentuser %}
    <h1>Welcome!</h1>
{% endifequal %}
  • 和 {% if %} 类似, {% ifequal %} 支持可选的 {% else%} 标签
  • 只有模板变量,字符串,整数和小数可以作为 {% ifequal %} 标签的参数。
  • 其他任何类型,例如Python的字典类型、列表类型、布尔类型,不能用在 {% ifequal %} 中。

注释

单行注释文章源自十年又十年-https://www.bbigsun.com/464.html

{# This is a comment #}

多行注释文章源自十年又十年-https://www.bbigsun.com/464.html

{% comment %}
This is a
multi-line comment.
{% endcomment %}

过滤器

{{ name|lower }}
{{ my_list|first|upper }}
{{ bio|truncatewords:"30" }}
{{ pub_date|date:"F j, Y" }}

include

{% include 'includes/nav.html' %}

block

使用 {% block %} 定义模板,block 告诉程序,不要写死,将来必改。文章源自十年又十年-https://www.bbigsun.com/464.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    <h1>My helpful timestamp site</h1>
    {% block content %}{% endblock %}
    {% block footer %}
    <hr>
    <p>Thanks for visiting my site.</p>
    {% endblock %}
</body>
</html>

extends

使用 {% extends %} 进行模板继承文章源自十年又十年-https://www.bbigsun.com/464.html

{% extends "base.html" %}

{% block title %}The current time{% endblock %}

{% block content %}
<p>It is now {{ current_date }}.</p>
{% endblock %}

纸上得来终觉浅,绝知此事要躬行。

weinxin
17688689121
我的微信
微信扫一扫
Django最后更新:2024-5-11
BBigSun
  • 本文由 BBigSun 发表于 2023年 4月 19日 15:15:05
  • 转载请务必保留本文链接:https://www.bbigsun.com/464.html
    • sseaa
      sseaa 0

      comment?x-oss-process=image/resize,m_fill,h_800,w_1350″ rel=”nofollow ugc”>

    匿名

    发表评论

    匿名网友

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定