#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
Смотрите видео HTML Template Inheritance in Django - Python Web Development онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь codevev 05 Июль 2021, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 736 раз и оно понравилось 36 людям.