In the web page design, there are several pages using a similar design structure. Django templates can support the creation of a master template. Then, each page can customize the master template by extending the master template. The {% extends %}
tag is used to extend the master template.
Here are the key steps to make extended templates.
{% block name %}
and {% endblock %}
for the section that you want to customizechild.html
{% extends "parent.html" %}
in the beginning of the file{% block name %}
and {% endblock %}
By implementing this, when the child.html template is rendered, parent.html and child.html are integrated and displayed in the browser as one HTML document.
In this practice, we'll demonstrate a template extension.
Assuming the base template is used by several applications in the project, we create the base.html file directly under the templates directory.
Open the base.html file and edit the following three points.
{% block body %}
... {% endblock %}
part will be overwritten by the other HTML files.
<a href="{% url 'course_list'%}">List</a>
<a href="{% url 'course_create'%}">Create</a>
{% block body %}
<p>Insert body contents here</p>
{% endblock %}
<h6>2023 Employee Learning</h6>
Open the template file and do the following:
{% extends "base.html" %}
tag in the begining{% block body %}
and {% endblock %}
tagsThe code below is an example of the create page template. The yellow lines are the new code.
{% extends "base.html" %}
{% block body %}
<h1> CREATE Page </h1>
:
</form>
{% endblock %}
After repeating the same thing for all the files, access to localhost:8000/employee-learning/course-list/. You can see that the pages still display the same content, although you have changed the template structure.
In the web page design, there are several pages using a similar design structure. Django templates can support the creation of a master template. Then, each page can customize the master template by extending the master template. The {% extends %}
tag is used to extend the master template.
Here are the key steps to make extended templates.
{% block name %}
and {% endblock %}
for the section that you want to customizechild.html
{% extends "parent.html" %}
in the beginning of the file{% block name %}
and {% endblock %}
By implementing this, when the child.html template is rendered, parent.html and child.html are integrated and displayed in the browser as one HTML document.
In this practice, we'll demonstrate a template extension.
Assuming the base template is used by several applications in the project, we create the base.html file directly under the templates directory.
Open the base.html file and edit the following three points.
{% block body %}
... {% endblock %}
part will be overwritten by the other HTML files.
<a href="{% url 'course_list'%}">List</a>
<a href="{% url 'course_create'%}">Create</a>
{% block body %}
<p>Insert body contents here</p>
{% endblock %}
<h6>2023 Employee Learning</h6>
Open the template file and do the following:
{% extends "base.html" %}
tag in the begining{% block body %}
and {% endblock %}
tagsThe code below is an example of the create page template. The yellow lines are the new code.
{% extends "base.html" %}
{% block body %}
<h1> CREATE Page </h1>
:
</form>
{% endblock %}
After repeating the same thing for all the files, access to localhost:8000/employee-learning/course-list/. You can see that the pages still display the same content, although you have changed the template structure.