E
E
Evgeny_A2021-05-23 16:34:50
Django
Evgeny_A, 2021-05-23 16:34:50

How to move a template out of a loop into a separate file in Django?

Welcome all.

I have loops like this in different places:

{% for object in object_list %}
    <h1>object.title</h1>
{% endfor %}


It became not convenient to edit every time the part that is inside the loop and I wanted to take it out:

<h1>object.title</h1>

In a separate item_in_loop.html file There are

two problems:

1. How to include item_in_loop.html in any template?
2. How to pass an object from the loop to item_in_loop.html?

Thanks for the hints.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Melnikov, 2021-05-23
@Mi11er

It is possible through include
{% include 'componentns/sweetallerts.html' %}

Y
Yupiter7575, 2021-05-23
@yupiter7575

<h1>object.title</h1>

This won't work.
How to include item_in_loop.html in any template?

Write extends at the beginning of the file
How to pass an object from the loop to item_in_loop.html?

It seems like all variables should be transferred
. In general, why create a new file because of one line?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question