django 模板过滤器:计算对象
我在 models.py Region 和 Survey 中有 2 个类。调查有 region_select=model.ForeignKey(region)
和其他一些属性,而区域只有 name=models.CharField
。使用 django 通用视图,我创建了一个项目/区域列表,并且我想要过滤这些项目的数量。我的模板如下所示:
<..>
<ul>
{% for Region in regions_list %}
<p><b>{{ Region }}</b> <i>(count: {{ ??? }} )</i><br>
{% for Survey in object_list %}
{% if Survey.region_select = Region %}
<a href="{% url region-display Region.id %}">{{ Survey }}</a>
{% endif %}
</p>
{% endfor %}
{% endfor %}
</ul>
<..>
region_list 是 Region.objects.all()
的查询集 与对象列表相同 (Survey.objects.all()
) 到目前为止,我得到了完美的列表,尽管我想分别计算每个区域的这些项目。 我已经尝试过 object_list.filter(region_select=Region).count
但写入任何内容到过滤器最终都会出现模板错误,说它无法解析我提供给过滤器的内容。有什么建议去 ???
地方吗?自定义 {{name|filter}} 过滤器怎么样?再次,我只需要它会计算对象...哦,我也尝试添加具有相同 Survey.objects.filter(region_select=Region).count
的方法,但它最终写了一个位置和类型那些物体(?!?)。
PS它不是复制/粘贴,因此重写时可能会出现简单的错误。
I have 2 classes in models.py Region and Survey. Survey have region_select=model.ForeignKey(region)
and some other attributes and region just have name=models.CharField
. Using django generic views i make a list of items/region and i would like to have a filtered number of those items. My template looks like:
<..>
<ul>
{% for Region in regions_list %}
<p><b>{{ Region }}</b> <i>(count: {{ ??? }} )</i><br>
{% for Survey in object_list %}
{% if Survey.region_select = Region %}
<a href="{% url region-display Region.id %}">{{ Survey }}</a>
{% endif %}
</p>
{% endfor %}
{% endfor %}
</ul>
<..>
region_list is queryset of Region.objects.all()
same with object list (Survey.objects.all()
)
So far i get perfect list though i want to count those items separately for each region.
I've tried object_list.filter(region_select=Region).count
but writing anything to filter ended up with template error saying that it could not parse what i give to filter. any suggestions what goes to ???
place? What about custom {{name|filter}} filters? again i just need that it would count objects... oh, i also tried adding method with same Survey.objects.filter(region_select=Region).count
but it ended up writing a place and type of those objects(?!?).
P.S. its not copy/paste so simple mistakes may have occurred while re-writing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
Try: