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
WarmtepompJune 16, 2019, 10:12 p.m.

It's outstandingly a confusing and key bit of information. I'm in stunning spirits that you in a general sense offered this astonishing information to us

Reply
WarmtepompJune 17, 2019, 5:28 a.m.

I went over this site and I trust you have a great deal of brilliant data, spared to my bookmarks

Warmtepomp

Reply
izlom camoJune 18, 2019, 12:31 a.m.

It's exceptionally a confounding and key piece of data. I'm in shocking spirits that you in a general sense offered this surprising data to us

Reply
ahjmedJune 19, 2019, 1:35 p.m.

This site is momentous data and certainties it's extremely brilliant

Warmtepomp verwarming

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
johnApril 23, 2019, 11:47 a.m.

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. text chemistry review

Reply
liamApril 23, 2019, 3:43 p.m.

TechDu is highly focused on publishing articles related to technology, Digital marketing, Data Science, Education, Product Reviews, Tips and Data Analytics. Product Reviews

Reply
frankApril 25, 2019, 10:54 a.m.

That's what makes you the best. You should keep publishing more articles and you will Such Become One of the best writers ever Latest Ghana Music

Reply
Sumit SinghApril 25, 2019, 12:18 p.m.

Best Tips for Searching Professional Packers and Movers for Stress Free Shifting
More info :- packers and movers in Pune, packers and movers charges, packers and movers Pune Charges, packers and movers charges in Pune</a

Reply
Sumitsingh72April 25, 2019, 12:19 p.m.

Significance of Packing Goods Rightly for Damage Free Transportation and Delivery More info :- packers and movers Delhi charges, packers and movers in Hyderabad, packers and movers Hyderabad Charges, packers and movers charges in Hyderabad

Reply
davidApril 28, 2019, 7:16 p.m.

Thanks for sharing this quality information with us. I really enjoyed reading. Will surely going to share this URL with my friends How to tie a man bun

Reply
davidApril 30, 2019, 6:15 p.m.

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. voyance-retour-amour.com

Reply
liamMay 2, 2019, 6:30 p.m.

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. youtube converter

Reply
davidMay 3, 2019, 11:55 a.m.

This is exactly what I was looking for. Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this! animeflv

Reply
kevinMay 3, 2019, 4:47 p.m.

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates. Projectsdeal UK Review

Reply
rickMay 3, 2019, 7:59 p.m.

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site. Jack in the box breakfast menu

Reply
erickMay 4, 2019, 1:51 p.m.

Thanks for ones marvelous posting! I genuinely enjoyed reading it, you are a great author. I will be sure to bookmark your blog and may come back very soon. I want to encourage you to definitely continue your great job, have a nice weekend. sportdog anti bark collar

Reply
johnMay 4, 2019, 7:59 p.m.

To say the truth I am very impressed by what you told. You share tons of interesting info, neat and excellent design you’ve got here. It’s certainly one of the most informative stuff on this topic I’ve ever read. Hypoxi reviews

Reply
karenMay 6, 2019, 7:14 p.m.

This is a really good post this. Truly, a magnificent art is making an article. And you are quite a master at that art. I would tell you to keep on continuing this awesome work of yours Facw'

Reply
johnMay 7, 2019, 7:42 p.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. Yard gnomes

Reply
Click Here!May 28, 2019, 6 a.m.

Some really helpful information in there. Why not hold some sort of contest for your readers? Click Here!

Reply
jamesMay 20, 2019, 9:35 p.m.

Thanks for the nice blog. It was very useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post. techofist.com

Reply
ahmedMay 31, 2019, 1:41 p.m.

Decent Information. Extraordinary Share!!! Usefull data for me! Thkanks

Havervlokken

Reply
ahmedMay 30, 2019, 2:45 a.m.

A debt of gratitude is in order for the useful and supportive post, clearly in your blog everything is great

Hazelnoten

Reply
https://www.thetrever.top/e-brochure/May 31, 2019, 12:38 p.m.

Thank you for the sensible critique. Me and my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more from this post. I’m very glad to see such fantastic information being shared freely out there. https://www.thetrever.top/e-brochure/

Reply
piermont grandMay 31, 2019, 12:38 p.m.

the most common electrical supply that i always buy at the local store is the elecrtical tape and electrical wire` piermont grand

Reply
sweet zzzJune 1, 2019, 5:52 p.m.

The Natures Novel uses natural wool as fire retardant. This way the mattress passes fire laws and is not a fire hazard while staying healthy. Since its required by law, all mattresses must have a fire barrier. The law however does not restrict manufacturers from using chemicals as fire retardants, so most manufacturers take the cheap route. regards natural latex mattress

Reply
Tax AccountantsJune 3, 2019, 5:10 a.m.

It's exceptionally a confounding and key bit of information. I'm in amazing spirits that you in a general sense offered this shocking information to us

Tax Accountants

Reply
consolidate payday loansJune 4, 2019, 6:34 p.m.

I can’t believe the quantity of spam that your website is getting hit with. When you need any assist with deleting the spam comments please email me. consolidate payday loans

Marketing1on1.com has provided industry-standard web hosting and related services to hundreds of thousands of customers across the globe. Your website deserves the quality and care that we can provide, using cutting-edge tactics and strategies through our SEO strategy services. marketing1on1.com - Online Internet Marketing Services, Website Marketing Company

Reply
ahmedJune 6, 2019, 6:03 a.m.

Wow I can say this is another extraordinary article true to form of this blog.Bookmarked this webpage..

https://mails-guide.com/hotmail-login/

Reply
ahmedJune 6, 2019, 4:58 p.m.

I have perused your article, I locate the substance very fascinating and point by point, I trust sooner rather than later, you will have all the more intriguing and careful articles to give perusers understanding. Increasingly about this issue, Thank you for sharing this article.

Tax Advisors

Reply
Sara TaylorJune 9, 2019, 11:24 a.m.

After years of not sleeping well and not finding the right guide for natural products online, she decided to do the research so others don’t suffer when buying a new mattress as she did. For the past 3 years, she studied everything about sleep and natural product to put the best natural mattress buying guide online. regards best latex mattress

Reply
ahmedJune 11, 2019, 1:12 p.m.

I have take in alots of tips from that point. I value your endeavors and individuals exploit from your post. I likewise exhortation to individuals yo visit your blog. it's pleasant to discover a site the subtleties such a great amount of data about various craftsmen

Zwembad plaatsen

Reply
ahmedJune 12, 2019, 6:39 p.m.

It is extremely incredible post and I truly appreciate when I am perusing this article. Much obliged to you for offering this extraordinary post to us. I am extremely glad to see this.

Zwembadbouwer

Reply
Monoblok zwembadJune 13, 2019, 11:27 a.m.

It's phenomenally a cluttering and key bit of information. I'm in astonishing spirits that you in a general sense offered this staggering information to us

Monoblok zwembad

Reply
ahmedJune 13, 2019, 1:29 p.m.

Proceed with the superb great article, I simply perused couple of articles about this page.

Zwembad aanleggen

Reply
ahmedJune 15, 2019, 7:57 a.m.

I should concede that your post is truly fascinating. I have spent a great deal of my extra time perusing your substance. Much thanks to you a ton!

Zonnepanelen

Reply
ahmedJune 16, 2019, 12:57 p.m.

Such an extremely helpful article. Extremely intriguing to peruse this article.I might want to thank you for the endeavors you had made for composing this wonderful article.

Airco installatie

Reply
Katie MolinaJune 19, 2019, 11:13 a.m.

This is it I was looking for! Getting to know the best coupon deals out there. What a place to bargain your money-saving products that are been affiliated with famous Abine Discount Code companies bestowing rewards and helping people purchase best products at reasonable rates. Regretting myself to not be able to discover this place that offers huge discounts, cut-off avails, buy-one-get-one offers via coupons and vouchers offered by various online stores.

Reply
Airco installateurJune 19, 2019, 11:34 a.m.

It's outstandingly a bewildering and key bit of information. I'm in stunning spirits that you in a general sense offered this astonishing information to us

Reply
bilal khanJune 20, 2019, 9:31 a.m.

It's remarkably a befuddling and key piece of data. I'm in staggering spirits that you in a general sense offered this astounding data to us

Communiekleding

Reply
ahmedJune 20, 2019, 5:05 p.m.

Extraordinary Post... Decent offer for me, perhaps pleasant for all peruser of your post.. much obliged

Warmtepompen

Reply

Post New Comment