我有一个关于Python的问题。
,这是我的观点,
def index(request):
contacts = Contact.objects.all()
threeboxs = Threeboxes.objects.all()
return render(request, 'home/index.html', {'threeboxs': threeboxs, 'contacts': contacts})我们在PHP中显示的方式
for(k=0; k<3; k++){
some css class, i will add the K value eg:someclass_0
echo threeboxs[k].title;
echo threeboxs[k].description;
}我可以像这样显示Python代码。
{% for threebox in threeboxs%}
<h5>{{ threebox.title }}</h5>
<p>{{ threebox.description|linebreaks }}</p>
{% endfor %}但是这样做,我不能用循环更新css类。然后我试着用这种方法,但那不管用。
{% context['loop_times'] = range(0, 3)
for n in loop_times: %}
{{ threebox[n].title }}
{% endfor %}有人能给我一个关于我该怎么做的建议吗?
发布于 2015-10-27 04:47:03
在遍历列表( templatetag )时,您正在尝试遍历一些指定的值。
{% for threebox in threeboxs%}
<h5 style="{% cycle 'class_1' 'class_2' 'class_3' %}">{{ threebox.title }}</h5>
<p>{{ threebox.description|linebreaks }}</p>
{% endfor %}这会让你:
<h5 style="class_1">Title 1</h5>
<p>Body 1</p>
<h5 style="class_2">Title 2</h5>
<p>Body 2</p>
<h5 style="class_3">Title 3</h5>
<p>Body 3</p>
<h5 style="class_1">Title 4</h5>
<p>Body 4</p>
<h5 style="class_2">Title 5</h5>
<p>Body 5</p> 等等..。
发布于 2015-10-27 04:40:21
尝试:
{% for iter,threebox in threeboxs.items%} 如果你需要的话可以让你进入钥匙..。甚至更好:
forloop.counter查看以下文档:https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for
你想用css类做什么?
https://stackoverflow.com/questions/33359901
复制相似问题