我确信我错误地说了这个问题,但是这里...... 我有一个在conference_list.html中显示的会议列表以及与会者数量。 。他们的头衔显得很好......然而,每次会议的与会者人数都相同。
Conference 1 Attendees 2
Conference 2 Attendees 2
Conference 3 Attendees 2
Conference 4 Attendees 2
第4次会议是唯一有2位与会者的会议。其他人有不同的金额。
Views.py:
@login_required
def conference_list(request):
try:
session_notification = request.session['notification']
del(request.session['notification'])
except:
session_notification = None
PAGE_SIZE = 20#number of conferences per page
page = int(request.GET.get('page', 1))
upper_bound = page * PAGE_SIZE - 1
lower_bound = (page - 1) * PAGE_SIZE
context = base_context(request)
network = context['network']
conferences = Conference.objects.all()
second = []
result = []
counter = 0
for conference in conferences:
is_attending = False
if counter < lower_bound or counter > upper_bound:
counter += 1
result.append(None)
continue
result_item = {}
result_item['conference'] = conference
result.append(result_item)
counter += 1
attendees = conference.investors.all()
count = attendees.count()
if request.user in attendees:
is_attending = True
context['attendees'] = attendees
context['is_attending'] = is_attending
context['count'] = count
context['current'] = 'conferences'
context['conferences'] = result
return render_to_response('conference_list.html', context, context_instance=RequestContext(request))
模板:
{% for conference in conferences %}
<p>{{ conference.conference.name }}</p><span>{{ count }}</span>
{% endfor %}
为什么会这样?为什么我不能显示适当的金额?
答案 0 :(得分:3)
您的count变量只保存最后一个值,因为您只是在视图中循环的每次迭代时覆盖它。试试result_item['conferences']['count'] = count
并在其后移动result.append(result_item)
行。相应地调整模板。