【发布时间】:2012-12-25 08:02:59
【问题描述】:
我正在尝试将查询的结果存储在列表或类似列表中(尝试不同的替代方案),但我得到最远的一个是:
def index(request):
for post in blog_posts:
comments_total.setdefault(post.id, []).append(comments.total(post.id))
return render_to_response('blog/index.html', {
'comments': comments_total})
在我得到的返回数据中: {5L:[2],6L:[1]}
我正在使用此代码访问:
{% if comments %}
{{comments}}<br />
{% for cid in comments %}
{% if cid == post.id %}
{{cid}} - {{comments.cid.0}}<br />
{{cid}} - {{comments.6.0}}
{% endif %}
{% endfor %}
{% endif %}
整体打印出来的是:
{5L: [2], 6L: [1]} 5 - 5 - 1
对于如何获取所有 cmets(在这种情况下为博客),是否有替代方法计算每个帖子的结果并将其返回到模板?
我正在尝试为博客的起始页获取计数器。 “你在这个帖子上有 (X) 个 cmets”——诸如此类。
【问题讨论】:
-
您是否在评论系统中使用任何特定模块?其中一些(如 Disqus)内置了用于返回此类信息的函数。
-
没有额外的模块。我唯一拥有的模块是我为 cmets 和帖子定制的,相当简单的“将其整齐地加载到数据库中”。
标签: django list templates object loops