Части в Шаблонах Django типо как в React
У меня есть два файла html и обоих есть одинаковые блоки с классом product.
<!-- shop/templates/favorite.html, shop/templates/index.html -->
...
<div class="product">
<div class="d-flex flex-column justify-content-between">
<div class="product__image-title">
<img class="product__image" src="{{ product.first_image }}" alt="">
<h5 class="product__title">
<a class="lnk" href="{{ product.get_absolute_url }}">{{ phone.name }}</a>
</h5>
</div>
<h4 class="product__price">{{ product.get_price }}</h4>
</div>
<div class="product__buttons">
<div>
<button type="submit" class=" btn rounded-0" data-to="{{product.to_favorite_url}}">
<i
class="bi bi-heart{% if phone in user.profile.favorite_products.all %}-fill text-primary{% endif %}"></i>
</button>
<button type="submit" class="btn rounded-0" data-to="{{product.to_compare_url}}">
<i class="bi bi-filter {% if product in user.profile.compare_products.all %}text-primary{% endif %}"></i>
</button>
</div>
<button class="btn rounded-0 btn-primary">
<i class="bi bi-basket"></i>
</button>
</div>
</div>
...
Можно ли сделать так что-бы объявить этот блок в отдельный html файл? И я мог вставить в favorite.html и index.html этот файл с контекстом?
class Product:
...
def as_card(self) -> str:
# нужен функция типо render_part
return render_part('product_card.html', {
'product': self
})
...
<!-- shop/templates/favorite.html, shop/templates/index.html -->
{% autoescape off %}
{{product.as_card}}
{% endautoescape %}