Now in most applications you'll want all the pages to have a similar look and feel, with changing data. But they're unlikely to be so much the same that a single template will cover all of your needs. Take, for example, a system which displays data either in the form of summary list of all records, or it displays the full data for a single record. That's where nested templates are useful.

1. You create a template which covers all the parts of the page common to all (in our example, both types of) pages. And in that template, you place named blocks:

The block names are your choice of word; the text within the block is a default value (great for debugging) which is displayed only if there is no available block of this name in the calling template file. Now - save that template file in a file which is not referenced in any way in the view controller Python - the link will come in a minute.

2. Create another template for the first of your page types - here's the index page template for my example:

You'll now find that when you render your page in the view controller, passing in your Context object, that the page you call up will load the base page from which it extends. Very near, and avoid the need for repeating yourself; Python's "DRY" philosophy extended to Django.

I have just finished a Python with Django course in Dublin and I'm awaiting my flight at the airport on my way home. We run public Python courses at our UK training centre (we have one starting on Monday!), and private Python courses onsite anywhere in Europe.

If you're looking to learn about the Django web framework, we don't run Public Django Training Courses, but with prior notice we can add an extra day onto the end of our public Python course, or include coverage within private course as we have done this week. Please get in touch!

P.S. We have lots more resources on Django ;-) including a complete set of files showing the above code and changes in context [here] on our web site. (written 2012-03-02, updated 2012-03-10)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles

This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.