Django Tip: Templates and Page Titles

Posted on April 18, 2014

The core Django philosophy is DRY: do not repeat yourself. This quick tip will save you time and headaches when writing Django templates with page titles.

Let's start off with some assumptions. You have a parent template that you reuse throughout the site. Keep it simple and imagine these are the contents of site.html:

<head>
  <title>
    {% block title %}{% endblock %}
  </title>
</head>
<body>
  {% block body %}{% endblock %}
</body>

Now typically, you would use it with a child template, as shown below in child.html:

{% extends "site.html" %}

{% block title %}My page title{% endblock %}

{% block body %}
<h1>My page title</h1>
<p>My page content</p>
{% endblock %}

Or perhaps, your page content came from the database, as shown below in dyanmic_page.html:

{% extends "site.html" %}

{% block title %}{{ page.title }}{% endblock %}

{% block body %}
<h1>{{ page.title }}</h1>
<p>{{ page.content }}</p>
{% endblock %}

Simple. But we're repeating ourselves. Notice we wrote My page title twice.

Learning Django? Subscribe to my Django articles, tips and tutorials.

Fortunately, it turns out that Django actually outputs the contents of a {% block %} tag when you invoke it inside another {% block %} tag.

That means you can remove the duplicate titles, as shown below:

{% extends "site.html" %}

{% block body %}
<h1>{% block title %}My page title{% endblock %}</h1>
<p>My page content</p>
{% endblock %}

Or for the dynamic template, like so:

{% extends "site.html" %}

{% block body %}
<h1>{% block title %}{{ page.title }}{% endblock %}</h1>
<p>{{ page.content }}</p>
{% endblock %}

Simple as that. No more repeating yourself when adding page titles.

Tags:Django

Comments

Donovan WilsonJuly 2, 2015, 2:35 p.m.

So how do I get page titles from the database?

Reply
Donovan WilsonJuly 2, 2015, 2:36 p.m.

by the way thanks for this post.

Reply
Donovan WilsonJuly 2, 2015, 2:38 p.m.

For example what if the page is a list? Can you store all the titles in one place?

Reply
DrorMay 13, 2016, 7:16 a.m.

Where is the value of "page.content" coming from ?

Reply
silviogutierrezMay 13, 2016, 11:30 p.m.

From whatever context you called when rendering the template. Could be an object you retrieved from the database using generic views.

See: https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-display/#detailview

Reply
Seth LivingstonJuly 30, 2017, 3:45 p.m.

This is great; thank you.

Reply
William S. BowieMarch 31, 2019, 12:29 p.m.

This weekend has been a mixture best dissertation writing services of rest and deadlines for me. My great joy is writing, yet I find it one of my most challenging endeavors. I love it so much, it intimidates me. To paraphrase Eric Liddell in the movie, Chariots of Fire, "When I write, I feel His pleasure."

Reply
MikeApril 5, 2019, 4:26 p.m.

It is one of the best site that I have visited. Hope you will share more quality blog posts thank you.
Captain Lorca Leather Coat

Reply
karenApril 11, 2019, 11:42 a.m.

Excellent read, Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. yashodham school

Reply

Post New Comment