如何减少 Django 中 html 中的循环?
How to reduce for loops in html in Django?
我在 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
的代码)?谢谢,请留下任何问题。
很难说您的代码中哪里存在性能问题。 html 中的循环应该不会花费很多时间。也许您有很多数据库查询或 运行 一些繁重的方法。
尝试调查您的代码的哪一部分确实很慢。你可以 Silk profiler 这个
Silk is a live profiling and inspection tool for the Django framework. Silk intercepts and stores HTTP requests and database queries before presenting them in a user interface for further inspection:
安装
pip install django-silk
在settings.py
中添加以下内容:
MIDDLEWARE = [
...
'silk.middleware.SilkyMiddleware',
...
]
INSTALLED_APPS = (
...
'silk'
)
尝试找到花费最多时间的方法并对其进行优化。此外,您可以将 Silk 的结果添加到您的问题中。有助于解决问题
减少数据库命中率总是好的。在您的代码中,您正在迭代中访问数据库,因此如果循环 运行 1000 次,它将访问数据库 1000 次,这可以减少到像这样的一个查询:
classes = Class.objects.filter(
teacher=teacher, date__in=[each_date for each_date in dates_in_month
if each_date not in weekends]
).order_by('date','time')
然后您可以迭代 classes
查询集以继续其余代码。
同时让你的代码更具可读性,现在它很乱。
我在 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
的代码)?谢谢,请留下任何问题。
很难说您的代码中哪里存在性能问题。 html 中的循环应该不会花费很多时间。也许您有很多数据库查询或 运行 一些繁重的方法。
尝试调查您的代码的哪一部分确实很慢。你可以 Silk profiler 这个
Silk is a live profiling and inspection tool for the Django framework. Silk intercepts and stores HTTP requests and database queries before presenting them in a user interface for further inspection:
安装
pip install django-silk
在settings.py
中添加以下内容:
MIDDLEWARE = [
...
'silk.middleware.SilkyMiddleware',
...
]
INSTALLED_APPS = (
...
'silk'
)
尝试找到花费最多时间的方法并对其进行优化。此外,您可以将 Silk 的结果添加到您的问题中。有助于解决问题
减少数据库命中率总是好的。在您的代码中,您正在迭代中访问数据库,因此如果循环 运行 1000 次,它将访问数据库 1000 次,这可以减少到像这样的一个查询:
classes = Class.objects.filter(
teacher=teacher, date__in=[each_date for each_date in dates_in_month
if each_date not in weekends]
).order_by('date','time')
然后您可以迭代 classes
查询集以继续其余代码。
同时让你的代码更具可读性,现在它很乱。