将切片过滤器与来自 Django QuerySet 的上下文数据一起使用 [英] Using the slice filter with context data from a Django QuerySet
本文介绍了将切片过滤器与来自 Django QuerySet 的上下文数据一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用模板中的此 html 代码将模型中的列表拆分为两列:
<代码>{{value.Site}}</a></ul>{% 结束为 %}我打算使用 slice 标签来过滤列表,例如:
{%for value in object_list|slice:"10:20" %}
但是它不起作用,我认为这可能是因为我有上下文数据,即 {{value.Site}},而不是例如 {{Site}}.这是对应的视图:
class homeview(ListView):模板名称 = 'annual_means/home.html'def get_queryset(self):return AnnualMean.objects.values("Site", "url").distinct()
我需要做什么才能让切片工作?
解决方案我想,你需要的是:
<tr><th>URL</th><th>站点</th></tr>{% for value in object_list %}<tr><td><a href="/sites/{{value.url}}/">{{value.url}}</a></td><td>{{value.Site}}</td></tr>{% 结束为 %}
URL 和站点将显示为表格.
I am trying to split a list from my model across two columns, using this html code in the template:
< div class ="col-md-6" > {%for value in object_list %} <ul>< ahref="/sites/{{value.url}}/">{{value.Site}}</a></ul> {% endfor %}
I was planning to achieve this with the slice tag to filter the list, e.g.:
{%for value in object_list|slice:"10:20" %}
It does not work however, and I think it might be because I have context data i.e. {{value.Site}}, instead of just {{Site}} for example. This is the corresponding view:
class homeview(ListView): template_name = 'annual_means/home.html' def get_queryset(self): return AnnualMean.objects.values("Site", "url").distinct()
What do I need to do to get the slice to work?
解决方案I think, what you need is this:
<table> <tr> <th>URL</th> <th>SITE</th> </tr> {% for value in object_list %} <tr> <td><a href="/sites/{{value.url}}/">{{value.url}}</a></td> <td>{{value.Site}}</td> </tr> {% endfor %} </table>
URLs and Sites will be displayed as a table.
这篇关于将切片过滤器与来自 Django QuerySet 的上下文数据一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文