1

Tried looking at other solutions but i cannot resolve this issue. I have a problem where the django template will iterate through {% for cr in Courses %} but will only do so once outputting the multiple lines together. E.g. given a list [['3000', '1', '2458'], ['3000', '0', '2821']], one iteration will be the whole list and not the two items in the list.

In my django template i have:

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

And in my 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))

I think i have a small issue somewhere but i am struggling. Thanks

Alex Stewart
  • 730
  • 3
  • 12
  • 30

1 Answers1

2

IMO Courses looks like this:

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

Try this

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))
Silwest
  • 1,620
  • 1
  • 15
  • 29