gpt4 book ai didi

python - Django 模板不迭代列表

转载 作者:行者123 更新时间:2023-12-01 05:40:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com