Django Starter 5 — Master Template

Rubaiyat Rahim
1 min readMay 26, 2024

--

Using a master template to reuse common items in multiple pages.

Other parts of this series: Part 1 | Part 2 | Part 3 | Part 4 | Part 6

Create Master Template

  • Create the master template (myproject\players\templates\master.html) as follows:
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>

{% block content %}
{% endblock %}

</body>
</html>

Change Templates to use the Master Template

Changing All Players Template

  • Change the list of all players template (myproject\players\templates\all_players.html) as follows:
{% extends "master.html" %}

{% block title %}
Chelsea Football Club - List of all Players
{% endblock %}


{% block content %}
<h1>Players</h1>

<ul>
{% for x in allplayers %}
<li><a href="details/{{ x.id }}">{{ x.firstname }} {{ x.lastname }}</a></li>
{% endfor %}
</ul>
{% endblock %}

Changing Players Detail Template

  • Change the Players Detail template (myproject\players\templates\details.html) as follows:
{% extends "master.html" %}

{% block title %}
Player Details of {{ theplayer.firstname }} {{ theplayer.lastname }}
{% endblock %}


{% block content %}
<h1>{{ theplayer.firstname }} {{ theplayer.lastname }}</h1>

<p>Phone {{ theplayer.phone }}</p>
<p>Date of Birth: {{ theplayer.dateofbirth }}</p>

<p>Back to <a href="/players">Players</a></p>

{% endblock %}

--

--