#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.
In the child templates, first put "{% extends "template.html" %} at the top of the file, where "template.html" is the name of your base template. Then, override the block from the base template like this:
{% block template_body %}
YOUR CUSTOM HTML GOES HERE
{% endblock %}
Remember that "template_body" is the name of the block from the base template.
-------------------------------------------------------------------------------------
Links:
Blog: https://evgenyurubkov.com
Twitter: / evgenyurubkov
Instagram: / evgenyurubkov
Youtube: / @codevev
Watch video HTML Template Inheritance in Django - Python Web Development online without registration, duration hours minute second in high quality. This video was added by user codevev 05 July 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 736 once and liked it 36 people.