2013-07-24 79 views
1

嘗試查看其他解決方案,但我無法解決此問題。我有一個問題,Django模板將遍歷{% for cr in Courses %},但只會一次輸出多行。例如。給定一個列表[['3000', '1', '2458'], ['3000', '0', '2821']],一個迭代將是整個列表,而不是列表中的兩個項目。Django模板無法遍歷列表

在我的Django的模板,我有:

<table> 
     <tr><th>Course</th><th>Payment Status</th><th>Unit</th><th>Action</th></tr> 
     {% for course in Comp_Course %} 
      <tr><td>{{ course }}</td><td></td><td></td><td></td></tr> 
      {% for cr in Courses %} 
       {% if course == cr.0 %} 
        <tr> 
         <td></td> 
         <td> 
          {% if cr.1 == "1" %} 
           Paid 
          {% else %} 
           Not Paid 
          {% endif %} 
         </td> 
         <td> 
          {{ cr.2 }} 
         </td> 
         <td> 
         </td> 
        </tr> 
       {% endif %} 
      {% endfor %} 
     {% endfor %} 
    </table> 

在我views.py

courses = [] 
    comp_course = [] 
    for payment in transactions: 
     if payment.payment_type == "1": 
      unit = Units.objects.get(webducate_id=str(payment.course)) 

      comp_course.append(str(unit.course.webducate_id)) 

      units = Units.objects.filter(course=unit.course) 
      unit_list = [] 
      for unit in units: 
       if unit.webducate_id == payment.course and payment.successfull == "1": 
        unit_list.append([str(unit.course.webducate_id),'1',str(unit.webducate_id)]) 
       else: 
        unit_list.append([str(unit.course.webducate_id),'0',str(unit.webducate_id)]) 
      courses.append(unit_list) 
    comp_course = list(set(comp_course)) 
    return render_to_response('student-account.html', {'Courses': courses, 'Comp_Course': comp_course,'Message': "", 'Transactions': transactions}, context_instance=RequestContext(request)) 

我想我有一個小問題的地方,但我在努力。由於

+0

什麼是課程,如果你打印出結果勸慰價值? –

+0

對不起@MattWritesCode,請檢查下面的解決方案。 –

回答

2

IMO課程是這樣的:

[[[a,b,c],[d,e,f]]]

試試這個

courses = [] 
comp_course = [] 
for payment in transactions: 
    if payment.payment_type == "1": 
     unit = Units.objects.get(webducate_id=str(payment.course)) 

     comp_course.append(str(unit.course.webducate_id)) 

     units = Units.objects.filter(course=unit.course) 
     unit_list = [] 
     for unit in units: 
      if unit.webducate_id == payment.course and payment.successfull == "1": 
       courses.append([str(unit.course.webducate_id),'1',str(unit.webducate_id)]) 
      else: 
       courses.append([str(unit.course.webducate_id),'0',str(unit.webducate_id)]) 
comp_course = list(set(comp_course)) 
return render_to_response('student-account.html', {'Courses': courses, 'Comp_Course': comp_course,'Message': "", 'Transactions': transactions}, context_instance=RequestContext(request)) 
+0

感謝您的幫助 –