Answer the question
In order to leave comments, you need to log in
How to speed up a Django template?
There is this template:
{% for o1 in o11 %}
<span>{{o1.n}}</span>
{% for o2 in o22 %}
{% if o2.o1 == o1 %}
<span>{{ o2t.n}}</span>
{% for t in tt %}
{% if t.o == o2 %}
<a href="/{{ t.u }}/">{{ t.n }}</a><span>({{ t.c }})</span>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
Answer the question
In order to leave comments, you need to log in
No matter how you twist it, you will have 1 million iterations, which is a lot. From more or less real options - take this code out of the template (generate a flat list in the controller, and then pass it to the template). Those. as a result you should have something like:
{% for el in l %}
<span>{{o1.n}}</span>
{% if el.flag1 %}
...
{% if el.flag2 %}
...
{% endif %}
{% endif %}
{% endfor %}
O!!! It is necessary to make convolution of objects!
from collections import defaultdict
_t_by_o2 = defaultdict(list)
for t in tt:
_t_by_o2[t.o].append=t
_o2_by_o1 = defaultdict(list)
for o2 in o22:
_o2_by_o1[o2.o1].append=o2
{% for o1 in o11 %}
<span>{{o1.n}}</span>
{% for o2 in _o2_by_o1.get(o1,[]) %}
<span>{{ o2t.n}}</span>
{% for t in _t_by_o2.get(o2,[]) %}
<a href="/{{ t.u }}/">{{ t.n }}</a><span>({{ t.c }})</span>
{% endfor %}
{% endfor %}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question