I’m sure I worded the question incorrectly, but here goes…
I have a list of conferences being displayed in conference_list.html as well as the amount of attendees. . They’re titles show up fine… However, the amount of attendees are the same for every conference.
Conference 1 Attendees 2
Conference 2 Attendees 2
Conference 3 Attendees 2
Conference 4 Attendees 2
Conference 4 is the only conference that has 2 attendees. The others have different amounts.
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))
Template:
{% for conference in conferences %}
<p>{{ conference.conference.name }}</p><span>{{ count }}</span>
{% endfor %}
Why is this happening? Why can’t I display the proper amount?
Your count variable only holds the last value, since you simply overwrite it on each iteration of your loop in your view. Try
result_item['conferences']['count'] = countand move the lineresult.append(result_item)after it. Adjust your template accordingly.