2012-04-10 71 views
-1

我敢肯定,我的措辭問題正確,但在這裏不用... 我有顯示在conference_list.html以及參加者的數量會議的列表。 。他們的標題顯示正常......但是,每個會議的與會者數量都是相同的。Python在循環列表中顯示相同的值。

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 %} 

這究竟是爲什麼?爲什麼我不能顯示適量?

回答

3

您的計數變量只保留最後一個值,因爲您只需在視圖中循環的每次迭代中覆蓋它。嘗試result_item['conferences']['count'] = count並在其後移動行result.append(result_item)。相應地調整您的模板。

+0

'異常類型:\t KeyError異常 異常值:\t conferences' – Modelesq 2012-04-10 16:16:23

+0

@Modelesq所以,你要布爾汗寫你確切的代碼嗎? – Marcin 2012-04-10 16:39:42

+0

@Marcin不,我不知道那是什麼錯誤。 – Modelesq 2012-04-10 16:44:08