进入我的模板second / threeth对象的最佳方法是什么?

时间:2013-10-15 12:28:46

标签: django

def myview(request):
    myobj = MyObject.objects.all()
    return render_to_response('x.html',{'myobj': myobj}, context_instance=RequestContext(request))

{% for o in myobj %}
    {{o.test}}
{% endfor %}

在我的模板中使用second / threeth对象的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

for has forloop attribute

{% for o in obj_queryset %}
    {% if forloop.counter == 2 %}Second{% else %}
    {% if forloop.counter ==3 %}Third{% endif %}
        {{ o.test }}
    {% endif %}
{% endfor %}

如果您只是从查询集中查找第二个对象(而不是打印其他任何内容),您只需查找第二个对象(see the docs for more about this):

{{ obj_queryset.2 }}

请注意,在您的示例中,myobj不是对象(即模型实例),它是一个查询集。

相关问题