python test development django-68.templates template tag {% for%}

Foreword

Some tags like this: {% tag%}, for example, need to start and end tags: {% tag%} ... Tag Content ... {% endtag%}, generally circular list for outputting content objects.

for labels

{% For%} allow us to iterate over a sequence. Python for statement and the situation is similar loop syntax is for item in iterator.
Each cycle, the template will render {% for%} everything between {% endfor%} and.

For example, write a template navigationbar.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% for name in name_list %}

<h1>
    <span>{{ name.type }}</span>
</h1>
{% endfor %}

</body>
</html>

views view

Function to view the contents list of the template to fill

from django.shortcuts import render
# 上海悠悠,QQ交流群:750815713

def navlist(request):
    name_list = [
        {
            "type": "科普读物",
            "value": ["宇宙知识", "百科知识", "科学世界", "生物世界"]
        },
        {
            "type": "计算机/网络",
            "value": ["Java", "Python", "C语言"]
        },
        {
            "type": "建筑",
            "value": ["标准/规范", "室内设计", "建筑科学", "建筑文化"]
        }
    ]

    context = {"name_list": name_list}

    return render(request, "navigationbar.html", context=context)

After running display

empty label

for ... empty ... enddor: for a label with optional clauses {% empty%}, and then given to the group is empty or not when it is found, a default value to

<body>

{% for name in name_list %}

<h1>
    <span>{{ name.type }}</span>
</h1>
{% empty %}
    <span> 无标签 </span>
{% endfor %}

</body>

When the value name_list empty, empty value displayed: No Label

from django.shortcuts import render

def navlist(request):
    context = {"name_list":[]}
    return render(request, "navigationbar.html", context=context)

Internal template variables forloop

{% For%} In the inner loop, the template can be accessed in a variable named forloop. This variable has a number of attributes, you can learn some of the information circulating through their process.

  • forloop.counter: represents the number of cycles. The value of this attribute from the beginning, so the first time through the loop, forloop.counter equal to 1.
  • forloop.counter0: forloop.counter0 and forloop.counter similar, but started from scratch. When the first cycle, a value of 0.
  • forloop.revcounter: forloop.revcounter value is an integer representing the number of elements in the remaining cycles. When the first cycle, the value of the total number of forloop.revcounter element in the sequence to traverse. When the last cycle, forloop.revcounter a value of 1.
  • forloop.revcounter0: forloop.revcounter0 and forloop.revcounter similar, but the index is zero-based. When the first cycle, the value forloop.revcounter0 is the number of elements in the sequence minus one. When the last cycle, forloop.revcounter0 value of zero.
  • forloop.first: forloop.first is a Boolean value, the first time through the loop to True. It is convenient when the first element requires special handling
  • forloop.last: forloop.last is a Boolean value, the last time through the loop to True. It is often placed between a pipe symbol set of links:
  • forloop.parentloop: nested loop, forloop.parentloop reference cycle parent objects forloop

To forloop.last for example, put the pipe symbol after each link, do not put the last one

<body>
{% for name in name_list %}

<h1>
    <span>{{ name.type }}</span>
</h1>
    {% for n in name.value %}
        <span>
        {{ n }}{% if not forloop.last %} | {% endif %}
        </span>
    {% endfor %}

{% empty %}
    <span> 无标签 </span>
{% endfor %}

</body>

display effect

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11801061.html