我正在尝试显示前5个最近发布的文件,但似乎没有出现。 当我通过使用Model.objects.all()获取所有对象时,它为我提供了来自Db的所有对象,但是当我试图获得前5个时,它不会显示。 请帮助并建议我。感谢
我的views.py是
def about_experiment(request, ex_link_name):
researcher = None
study = None
posts = None
exp = get_object_or_404(Experiment,link_name = ex_link_name)
high_scores = ScoreItem.objects.filter(experiment=exp,active=True)
context = {
'request': request,
'exp':exp,
'high_scores': high_scores,
'awards':AwardItem.objects.filter(experiment=exp,visible=True),
'posts':Help.objects.filter().order_by('-date')[0],
'documents':Help.objects.filter().order_by('-document')[:5]
}
if exp.about_file:
context['about_file'] = settings.EXPERIMENT_DIRS+exp.about_file.get_include_path()
if request.method == 'POST':
form = AboutHelp(request.POST, request.FILES)
if form.is_valid():
obj = form.save(commit = False)
obj.save()
return HttpResponseRedirect('/about/%s/' %ex_link_name)
#return redirect(reverse('lazer.views.about_experiment', kwargs={ 'ex_link_name':obj.link_name }))
else:
form = AboutHelp()
return render(request, 'about_experiment.html', context)
目标模板页面
<div class="tab-pane" id="irb">
<h4> List of file(s) uploaded:</h4>
<!--File upload-->
{% if documents %}
<ul>
<li><a href="#"> {{ documents.document }} </a></li>
</ul>
{% else %}
<p>No such documents available.</p>
{% endif %}
<!--File upload ends-->
答案 0 :(得分:1)
documents
是一个查询集,你正在挑选前5名。你需要这样做。
<div class="tab-pane" id="irb">
<h4> List of file(s) uploaded:</h4>
<!--File upload-->
{% for doc in documents %}
<ul>
<li><a href="#"> {{ doc.document }} </a></li>
</ul>
{% empty %}
<p>No such documents available.</p>
{% endfor %}
<!--File upload ends-->