filmov
tv
HTML Template Inheritance in Django - Python Web Development

Показать описание
#Python #Shorts
Even when building a simple app, we may end up with similar HTML pages that differ only slightly. If we didn't use Django, then we'd have to copy/paste all that HTML code and guess what? If we wanted to update something that's displayed globally in our app, we'd have to go and update each one of these HTML files!
But we're programmers, right? And programmers know that duplication is bad. Luckily, Django allows us to use what's called a template inheritance.
We will just define a base HTML template, put one or more blocks into it letting the children of the template know that they can override its content, and voila, now all we need to do is inherit this template and put a custom logic inside the block we want to override in our children templates.
Key things to know:
1. In the base template, use {% block template_body %}{% endblock %}, where "template_body" can be any other name.
{% block template_body %}
YOUR CUSTOM HTML GOES HERE
{% endblock %}
Remember that "template_body" is the name of the block from the base template.
-------------------------------------------------------------------------------------
Links:
Even when building a simple app, we may end up with similar HTML pages that differ only slightly. If we didn't use Django, then we'd have to copy/paste all that HTML code and guess what? If we wanted to update something that's displayed globally in our app, we'd have to go and update each one of these HTML files!
But we're programmers, right? And programmers know that duplication is bad. Luckily, Django allows us to use what's called a template inheritance.
We will just define a base HTML template, put one or more blocks into it letting the children of the template know that they can override its content, and voila, now all we need to do is inherit this template and put a custom logic inside the block we want to override in our children templates.
Key things to know:
1. In the base template, use {% block template_body %}{% endblock %}, where "template_body" can be any other name.
{% block template_body %}
YOUR CUSTOM HTML GOES HERE
{% endblock %}
Remember that "template_body" is the name of the block from the base template.
-------------------------------------------------------------------------------------
Links:
Комментарии