开发者

{% with %} templatetag to avoid query execution multiple times?

开发者 https://www.devze.com 2023-02-13 02:52 出处:网络
If I use for example {{ some_model.objects.all }} in several places in one template, will the query be executed each time? And if so, should I always use {% with some_model.objects.all as some_name %}

If I use for example {{ some_model.objects.all }} in several places in one template, will the query be executed each time? And if so, should I always use {% with some_model.objects.all as some_name %} to avoid that? I saw this in s开发者_开发百科everal apps, so I'm wondering if I understand it correctly.


Yes, it does repeat the query. You should populate the context in your view instead of calling these functions within the template.

Using {% with %} indeed does solve the problem though for future calls, but I still vote for the view.

PS: I highly suggest downloading django-debug-toolbar
https://github.com/dcramer/django-debug-toolbar

You can test these ideas in a second : )

{% with %} templatetag to avoid query execution multiple times?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号