这是我的代码。
obj_list=Location.objects.all()
first_element=obj_list[0]
last_element=obj_list[-1]
then,
return render_to_response(template_name, {
'first_element':first_element,
'last_element':last_element,
})
并在模板中:
{{ first_element.terminal_id}} {{last_element.terminal_id}}
但它什么也没显示,
我能做些什么 ,
thanks
看一下http://docs.djangoproject.com/en/dev/topics/db/queries/#limiting-querysets http://docs.djangoproject.com/en/dev/topics/db/queries/#limiting-querysets
负索引(即Entry.objects.all()[-1]
) 不支持。
Try:
first_element = Location.objects.all()[0]
last_element = Location.objects.all().reverse()[0]
-- 2017 年 8 月 6 日更新 --
根据@MisterRios 的评论,
从 1.6 开始 Django 支持使用.first()
and .last()
在查询集上:
first_element = Location.objects.first()
last_element = Location.objects.last()
Refer: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#django.db.models.query.QuerySet.first https://docs.djangoproject.com/en/1.7/ref/models/querysets/#django.db.models.query.QuerySet.first
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)