1

尝试查看其他解决方案,但我无法解决此问题。我有一个问题,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>

在我看来.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))

我想我在某个地方有一个小问题,但我很挣扎。谢谢

4

1 回答 1

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))
于 2013-07-24T09:56:55.110 回答