-1

我确定我的问题措辞不正确,但是这里有……我在 Conference_list.html 中显示了一个会议列表以及参加者的数量。. 他们的标题显示得很好......但是,每次会议的参加者数量都是相同的。

Conference 1        Attendees 2
Conference 2        Attendees 2
Conference 3        Attendees 2 
Conference 4        Attendees 2

会议 4 是唯一有 2 名与会者的会议。其他的金额不同。

视图.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 %}

为什么会这样?为什么我不能显示正确的金额?

4

1 回答 1

3

您的 count 变量仅保存最后一个值,因为您只需在视图中的循环的每次迭代中覆盖它。尝试result_item['conferences']['count'] = count移动result.append(result_item)它后面的线。相应地调整您的模板。

于 2012-04-10T16:04:02.670 回答