Django - 循环标签。我怎样才能让它发挥作用?

Django - Cycle Tag. How can I make it work?

我是 Django 的新手。我正在尝试实施循环标签。无济于事。 我的 view.py:

def music(request):
    my_list = ['Ravel', 'Bach', 'Verdi', 'Janacek']
    context ={'my_list': my_list}
    return render(request, 'music.html', context)

我的模板文件:

<head>
<style>
   .row1 {
         background: #FFFF00;
     }
   .row2 {
         background: #FF0000;
    }
</style>

<h1>Music</h1>

</head>
 <body>
{% for o in my_list %}<tr class="{% cycle 'row1' 'row2' %}"></tr>{% endfor %}
</body>

我做错了什么?

这实际上不是关于 Django 或循环标记的问题。

您需要在 table 行中添加一些内容才能在 HTML 中显示。 tr 需要包含一个或多个 td,而 td 又需要包含一些实际文本。此外,整个事情需要在 <table>.

<table>
{% for o in my_list %}
    <tr class="{% cycle 'row1' 'row2' %}">
        <td>{{ o }}</td>
    </tr>
{% endfor %}
</table>