小编Ava*_*is 的帖子

如何遍历 django 模板中的列表?

我将 4 个相同长度的列表和列表的长度传递给我的模板。如何遍历列表?

视图.py

def allbooks(request):
    ID = [1,2]
    bookName = ["Python", "Java"]
    author = ["idk", "who"]
    copies = [3,7]
    return render(request, 'allbooks.html',{'ID':ID,'bookName':bookName, 'author':author, 'copies':copies,'range':range(len(ID))})
Run Code Online (Sandbox Code Playgroud)

全书.html

{% extends 'base.html' %}

{% block content %}
{% if user.is_authenticated %}
<div>
    <h3>
        List of books:
    </h3>
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Book Name</th>
                <th>Author</th>
                <th>Number of copies</th>
            </tr>
        </thead>
        <tbody>
            {% for x in range %}
                <tr>
                    <td>{{id[x]}}</td>
                    <td>{{bookName[x]}}</td>
                    <td>{{author[x]}}</td>
                    <td>{{copies[x]}}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</div>    
{% else %}
    <h3>You …
Run Code Online (Sandbox Code Playgroud)

python django django-templates

6
推荐指数
1
解决办法
7877
查看次数

标签 统计

django ×1

django-templates ×1

python ×1