如何使用Django循环标签 [英] How To Use Django Cycle Tag
问题描述
'row1'
和'row2'
: {%for some in some_list%}
< tr class ={%cycle'row1''row2'%}>
...
< / tr>
{%endfor%}
其次,如何正确地将其实现到我的模板?我正在使用一个非常简单的 JS库,这将允许排序:
page.html
{%if Variable%}
< ; table class =sortable>
< tr>
< th>标题1<
< th>标题2<
< th>标题3<
< th>标题4<
< th>标题5<
< / tr>
{%for Variable in Variable%}
< tr class ={%cycle'stuff'%}>
< td>
< a href ={%url'detail_page'stuff.id%}>
{{stuff.Name | capfirst}}< / a>
< / td>
< / tr>
{%endfor%}
< / table>
{%else%}
< p>找不到结果< / p>
{%endif%}
我的models.py,以防您需要它: p>
def view(request):
Variable = database.objects.all()
context = {'Variable ':variable,
}
return render(request,'app / page.html',context)
编辑:似乎我有一个正确的代码,只是一些不平衡的应用CSS,使我的表不像一张桌子。循环标签不需要,只有for循环。在添加另一个表行后,它也看起来更好:
{%for Variable in Variable%}
< tr>
< td> {{stuff.Name | capfirst}}< / td>
< td> {{stuff.Number | capfirst}}< / td>
< / tr>
{%endfor%}
循环标签,两个参数将交替添加到模板中,例如模板中的代码 {%for some in some_list%}< tr class ={%cycle'row1''row2 '%}>< / tr> {%endfor%}
将生成:
< tr class =row1>
...
< / tr>
< tr class =row2>
...
< / tr>
< tr class =row1>
...
< / tr>
< tr class =row2>
...
< / tr>
<! - ...等等,而some_list中有元素 - >
等等。通常你会附加一些CSS,例如:
< style>
.row1 {
背景:#345678;
}
.row2 {
background:#123456;
}
< / style>
例如,这将给替代行不同的背景颜色。
This hopefully is a pretty easy question. My endgoal is to be able to view my database entries in a table which I can sort via the column headers. I have read the documentation on cycle tags, but don't know they mean by 'row1'
and 'row2'
:
{% for o in some_list %}
<tr class="{% cycle 'row1' 'row2' %}">
...
</tr>
{% endfor %}
Secondly, how would I correctly implement it into my template? I am using a very simple JS library, which will allow the sorting:
page.html
{% if Variable %}
<table class="sortable">
<tr>
<th>Title 1</th>
<th>Title 2</th>
<th>Title 3</th>
<th>Title 4</th>
<th>Title 5</th>
</tr>
{% for stuff in Variable %}
<tr class="{% cycle 'stuff' %}">
<td>
<a href="{% url 'detail_page' stuff.id %}">
{{ stuff.Name|capfirst }}</a>
</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No Results Found</p>
{% endif %}
my models.py in case you need it:
def view(request):
Variable = database.objects.all()
context = {'Variable': Variable,
}
return render(request, 'app/page.html', context)
EDIT: It seems I had the right code all along, just some unevenly applied CSS that made my table not look like a table. The cycle tag was not needed, only the for loop. It also looked better after adding another table row:
{% for stuff in Variable %}
<tr>
<td>{{ stuff.Name|capfirst }}</td>
<td>{{ stuff.Number|capfirst }}</td>
</tr>
{% endfor %}
For the cycle tag, the two arguments will be added alternately to the template, for example, the code in your template {% for o in some_list %}<tr class="{% cycle 'row1' 'row2' %}"></tr>{% endfor %}
would generate:
<tr class="row1">
...
</tr>
<tr class="row2">
...
</tr>
<tr class="row1">
...
</tr>
<tr class="row2">
...
</tr>
<!-- ... and so on while there are elements in some_list -->
And so on. Normally you would attach some css to this, for example:
<style>
.row1 {
background: #345678;
}
.row2 {
background: #123456;
}
</style>
This would give alternate rows different background colours, for example.
这篇关于如何使用Django循环标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!