Django模板没有迭代列表

时间:2013-07-24 07:17:46

标签: python django

尝试查看其他解决方案,但我无法解决此问题。我有一个问题,即django模板将遍历{% for cr in Courses %},但只有在一起输出多行时才会这样做。例如。给定一个列表[['3000', '1', '2458'], ['3000', '0', '2821']],一个迭代将是整个列表,而不是列表中的两个项目。

在我的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))

我想我在某个地方有一个小问题,但我正在努力。感谢

1 个答案:

答案 0 :(得分: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))