python - Django 模板不迭代列表

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

在我的观点中.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))

我认为我在某个地方遇到了小问题,但我正在努力。谢谢

最佳答案

IMO 类(class)如下所示:

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

关于python - Django 模板不迭代列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17827536/

相关文章:

python - Odoo XML RPC 搜索和插入

Python 递归尝试未返回任何值

python - 如何将 Django 模型字段分解为自己的模型并保留数据?

python - 运行时请求数据库密码 "manage.py test"

django - 如何将 django ManyToManyField 转换为 Django-nonrel 字段?

django - 根据 Django 表单中的其他字段显示表单字段

python - 替换 Numpy 数组中的多个值

python - 如何使用 Python 访问 Ring 0?

django - 如何使用 sql 2005 在 django models 1.3 中获取长文本字段

python - 使用 'utf-8' 解码但错误显示 Unicode 编码错误?