Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse same "block" of html in multiple django templates

Currently, I have two html template that extends from a base.html:

page1.html:

{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
Code_block_1
{% endblock %}

page2.html:

{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
Code_block_1
{% endblock %}

Both html share the same Code_block_1.

I was thinking about about creating another html called Code_block_1.html to consolidate this repeating piece of code. Then, insert Code_block_1.html into page1.html and pag2.html. Django only lets you extend once. How do I get around this problem?

Thanks.

like image 483
H C Avatar asked Oct 30 '25 11:10

H C


1 Answers

Simply create another HTML file called code_block_1.html and then inside both page1.html and page2.html use include like this:

<!-- page1.html -->

{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}


<!-- page2.html -->

{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}
like image 74
nik_m Avatar answered Nov 02 '25 06:11

nik_m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!