N
N
Neoliz2016-08-12 12:28:11
Django
Neoliz, 2016-08-12 12:28:11

Is it possible in django to cache the rendered part of a template on the fly?

The essence of the question is this, here is a small simplified example of a template. The bottom line is that there is such a situation when it occurs two or even more in the template. Those. when rendering, this code is executed two or even more times. Is it possible to render a piece once (during template rendering) and then insert the already rendered one?

{% if page_obj.paginator.num_pages >= 10 %}
    {# Если элементов больше чем 10 #}
    {# Боковые теги #}
    {% if page_obj.has_previous %}
        <li><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page=1">«</a></li>
    {% else %}
        <li class="disabled"><span>«</span></li>
    {% endif %}
    {# Сам цикл формирования кнопок с цифрами. Если их меньше 10 то просто выводим #}
    {% for i in page_obj.paginator.page_range %}
        {% if i|add:"5" >= page_obj.number and i|add:"-5" <= page_obj.number %}
            {% if i == page_obj.number %}
                <li class="active"><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page={{ i }}">{{ i }}</a></li>
            {% else %}
                <li><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page={{ i }}">{{ i }}</a></li>
            {% endif %}
        {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
        <li><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page={{ page_obj.paginator.num_pages }}">»</a></li>
    {% else %}
        <li class="disabled"><span>»</span></li>
    {% endif %}
    {# Конец формирования списка ссылко. Вторая кнопка стрелочка #}
{% else %}
    {# Если элементов меньше чем 10 #}
    {# Боковые теги #}
    {% if page_obj.has_previous %}
        <li><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page={{ page_obj.next_page_number }}">«</a></li>
    {% else %}
        <li class="disabled"><span>«</span></li>
    {% endif %}
    {# Сам цикл формирования кнопок с цифрами. Если их меньше 10 то просто выводим #}
    {% for i in page_obj.paginator.page_range %}
        {% if i == page_obj.number %}
            <li class="active"><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page={{ i }}">{{ i }}</a></li>
        {% else %}
            <li><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page={{ i }}">{{ i }}</a></li>
        {% endif %}
    {% endfor %}
    {# Конец формирования списка ссылко. Вторая кнопка стрелочка #}
    {% if page_obj.has_next %}
        <li><a href="{{ request.get_full_path }}{% if request.GET %}&{% else %}?{% endif %}page={{ page_obj.next_page_number }}">»</a></li>
    {% else %}
        <li class="disabled"><span>»</span></li>
    {% endif %}
{% endif %}
{# Конец пагинации #}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2016-08-12
@timofeydeys

https://docs.djangoproject.com/en/1.10/topics/cache/ Template fragment caching
section

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question