使用带有来自Django QuerySet的上下文数据的片过滤器 [英] Using the slice filter with context data from a Django QuerySet

查看:192
本文介绍了使用带有来自Django QuerySet的上下文数据的片过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < p>我正在尝试从两个列中分离一个列表,使用模板中的这个html代码: ; div class =col-md-6> 
{%for object_list%}
< ul>< AHREF = /位点/ {{value.url}} / > {{value.Site}}< / A>< / UL>
{%endfor%}

我打算用slice标签来实现这个过滤列表,例如:

  {%for object_list | slice:10:20%} 

它不起作用,我认为这可能是因为我有上下文数据,即{{value.Site}},而不仅仅是{{Site}}。这是相应的视图:

  class homeview(ListView):
template_name ='annual_means / home.html'

def get_queryset(self):
return AnnualMean.objects.values(Site,url)。distinct()

我需要做些什么才能使切片工作?

解决方案

我想,你需要的是:

 < table> 
< tr>
< th> URL< / th>
< S&S< / th>
< / tr>
{%for object_list%}
< tr>
< td>< a href =/ sites / {{value.url}} /> {{value.url}}< / a>< / td>
< td> {{value.Site}}< / td>
< / tr>
{%endfor%}
< / table>

网址和网站将显示为表格。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆