...

Open source softwares - Django

Back to Course

Lesson Description


Lession - #1050 Django-extends


Extends

The extends tag permits you to add a parent layout for the ongoing format.

This implies that you can have one master page that behaves like a parent for any remaining pages:

Example
mymaster.html:
<html>
<body>

<h1>Welcome</h1>

{% block mymessage %}
{% endblock %}

</body>
</html> 

template.html:
{% extends 'mymaster.html' %}

{% block mymessage %}
  <p>This page has a master page</p>
{% endblock %} 

You put placeholders in the master template, let Django know where to put which content.

Django utilizes the {% block %} tag, to make placeholders:

master.html:
<html>
<body>

{% block myheading %}
{% endblock %}

{% block mymessage %}
{% endblock %}

</body>
</html> 

Templates that utilizes the master template, utilizes the {% block %} tag to make content that will be shown in the placeholder with a similar name:

template.html:
{% extends 'mymaster.html' %}

{% block myheading %}
  <h1>Members</h1>
{% endblock %}

{% block mymessage %}
  <ul>
    {% for x in members %}
      <li>{{ x.firstname }}</li>
    {% endfor %}
  </ul>
{% endblock %}