Using the slice filter with context data from a Django QuerySet

匆匆过客 提交于 2019-12-02 23:05:51

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/42127491/using-the-slice-filter-with-context-data-from-a-django-queryset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!