Default and Specific Titles Using Django Blocks

If you are extending a base template, here's how to have a default or specific title on your page.

nicksensei
1 min read
May 27, 2021
If you have a base template base.html
and you want to have a default title most the time, but also provide specific titles, here's one way to do it.
In your base.html
, add a block named title inside of your <title>
tags:
<html>
<head>
<title>
{% block title %}Default Title{% endblock %}
</title>
...
The key here is to add a default title in-between {% block title %}
and {% endblock %}
Then in a template where you are extending base.html
, add a title block if you want to provide a custom title.
{% extends "base.html" %}
{% block title %}Custom Title{% endblock %}
...