Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper method for Django template inheritance of <head> content

I have a base.html template with sitewide tags for charset, google-site-verification, stylesheets, js.... I also need to set up blocks for page specific title tags and meta descriptions.

I am wondering, should I set up a {% block head %} in my base.html and in my inherited template mix tags in that block, or should i set up specific blocks such as {% block meta %} and {% block title %} so that the tags appear in their proper places when Django renders to html.

Does this make sense? If I view source with all the tags mixed in one {%block head %} things are a bit out of order, but if I add specific blocks for each tag they are in order but use much more code...?

like image 852
shipwreck Avatar asked Dec 13 '11 22:12

shipwreck


People also ask

How to use template inheritance in Django?

To use Template Inheritance we must first create a base template. A base template is just a simple skeleton that we will be used as a base for all other pages on our site. A base template defines areas where content can be inserted into. In Django, we define this area using the block tag.

What is a base template in Django?

A base template is just a simple skeleton that we will be used as a base for all other pages on our site. A base template defines areas where content can be inserted into. In Django, we define this area using the block tag. Create a new file named base.html inside the sitewide templates directory and add the following code to it.

How to create a base HTML file in Django app?

Let us create a base HTML file at the project level and then have the Django App templates inherit it. To make the base file accessible, add the following line into TEMPLATES in settings.py as shown in screenshot below. This line executes the following function: Then with the os module, we join it to the django_project/templates file.

How to use template inheritance to create a base template?

In the shell running the server you should see BASE_DIR value as follows: To use Template Inheritance we must first create a base template. A base template is just a simple skeleton that we will be used as a base for all other pages on our site. A base template defines areas where content can be inserted into.


1 Answers

I normally have three blocks. Those three have covered all my and my colleague's needs in the last 1.5 year :-)

  • A block for css.

  • A block for javascript.

  • A block called "head-extras". Often you want to do something special on a page-by-page basis like adding a link element that points at your rss feed. Or some inline javascript snippet. With this block, you allow these corner cases in a clear way.

In templates that extend the base template, you can use {{ super }} in the css and javascript blocks to get the "parent's" list and extend it with your own.

I also have a head block around the whole thing for those few cases where you just want to override everything in the head :-)

like image 174
Reinout van Rees Avatar answered Sep 18 '22 19:09

Reinout van Rees