【发布时间】:2020-09-18 11:08:56
【问题描述】:
我有一个对象的限制 10 的相关查询。在开发模式下加载整个页面需要 9s 以上。当我没有在模板上运行相关的循环查询时,加载只需 1 秒。我很困惑这里发生了什么!请帮我找出问题/我在这里做错了什么!提前致谢!
这是我的视图文件代码 -
related = News.objects.filter(
is_active=True,
status='public',
language=request.LANGUAGE_CODE,
category=news.category
).order_by('-published_at')[:10]
这是模板文件的循环 -
{% for r in related %}
<li class="row mb-4">
<a href="{% url 'single_news' r.id %}" class="col-5">
<img src="{{ r.featured_image.url }}" alt="Image" class="rounded img-fluid">
</a>
<div class="col-7">
<a href="{% url 'single_news' r.id %}" class="no-underline">
<h6 class="mb-3 h5 text-charcoal">{{ r.heading }}</h6>
</a>
<div class="d-flex text-small">
<span class="text-muted ml-1">{{ r.published_at }}</span>
</div>
</div>
</li>
{% endfor %}
这是查询时间的 django-debug-ttolbar 图片 -
【问题讨论】:
标签: python django django-templates django-queryset