【发布时间】:2022-01-25 20:13:30
【问题描述】:
我在 django html 中尝试了以下 for 循环和 if 语句,但加载一页需要很长时间。首先,这里是html:
{% for time in TIME_CHOICES %}
<tr class="bg-white border-b border-gray-400">
<td class="border-r border-gray-400 py-1 whitespace-nowrap text-sm font-medium text-gray-900">
{{time}}
</td>
{% for each_date in dates_in_month %}
{% if each_date not in weekends %}
{% for class in classes %}
<h1>class</h1>
{% endfor %}
{% else %}
<td class="py-1 whitespace-nowrap text-sm text-gray-500">
<ul class="list-none" style="font-size: 0.70rem; line-height: 0.85rem;">
<li>-----</li>
<li>--(--)</li>
</ul>
</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
我认为这是因为我的 html 中有太多的 for 循环和 if 语句。无论如何我可以提高速度吗?或者有什么方法可以在 django 视图中做同样的事情(我使用的是通用列表视图,所以我需要一些代码用于get_context_data)?谢谢,如有任何问题,请留下。
【问题讨论】:
-
不是
current_classes = classes.filter(date=each_date)每天都进行数据库查询,而是对所有天进行一次查询,然后在 Python 中对其进行排序?
标签: django django-models django-views django-forms django-templates