我有一个 django 模板,该页面将填充消息。我希望消息在 10 种颜色之间轮换。所以 1:red 2:blue .......10:yellow 1:red 。因此,如果我有 20 条消息,您将看到每种颜色两次。
这是我的 stream.html
{% extends "index.html" %}
{% load staticfiles %}
{% block content %}
{% for secret in secrets%}
<section id="blue" class="container-fluid text-blue">
<div class="container">
<div class="col-md-12 text-center">
<h3 class="">{{secret.secretmessage}}</h3>
</div>
</div> <!-- /container -->
</section>
{% endfor %}
{% endblock %}
这是我的看法:
def stream(request):
h = Secrets.object.all()
context = locals()
template = 'stream.html'
return render_to_response(template, context)
因为我不能在模板中声明变量,所以我不得不寻找这个解决方案。提前致谢
最佳答案
我做了以下操作并且效果很好:
{% extends "index.html" %}
{% load staticfiles %}
{% block content %}
{% for secret in secrets%}
<tr class="{% cycle "lightgreen" "purple" "darkgreen" "lightblue" "darkblue" "yellow" as rowcolors %}"></tr>
<section id="{{rowcolors}}" class="container-fluid messages">
<div class="container">
<div class="col-md-12 text-center">
<h3 class="">{{secret.secretmessage}}</h3>
</div>
</div> <!-- /container -->
</section>
{% endfor %}
{% endblock %}
谢谢里卡多
关于python - 如何使用 django for 标签在 10 种不同的背景颜色之间旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350340/