I am building my first Django website, after using Symfony 1.3.x to build a few website. Both frameworks employ the MVC pattern.
Symfony allows a ‘master’ template to be created (called the layout), and each individual view is then used to decorate the master layout, using the decorator pattern. This makes it very easy to design pages that use the same layour (e.g. header/footer etc), whilst differing on the actual page content.
I am looking to see if something similar is available in Django. So far, all of the examples I have seen involve creating seperate views. Doing things this way is not very DRY, as each of my view template is repeating a lot of common stuff (header/footer etc).
Is there a way to have a ‘master’ view (using Django terminology), that establishes the layout of the views that are applied to it?
From what you say I think that you used “view” instead of “template”. If you meant “template”, you should know that django has a solution to this problem: template inheritance. Basically, you should design a “base” template that would be the skeleton of every page, and ther particular templates would inherit that template. Here is a short example taken from the django docs:
The docs don’t have an example for
base_generic.html, but a simple one would look like this:Note that
Default titlefrombase_generic.htmlwill be replaced by{{ section.title }}from the template that extends it. If there was no title block in the template that extendsbase_generic.html, the title would have beenDefault title. This happens for all blocks.