Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
Nesting Templates in Django

Django's templates are HTML files into which your controller will fill web page content. This is done by means of an extra tagging language, which includes two groups of constructs:

a) construct starting with {{ and ending with }}, which are filled in with the value held in a field in the Context object.

  Copyright {{copyright}} in year {{year}}


b) formatting controls - keyword based commands starting with {% and ending with %}. There are a limited number (about 20) such commands.

  {% for colleague in clist %}
    &bull; How about asking {{colleague.0}}<br />
  {% endfor %}

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:

  <title>Staff Stuff</title>
  <body bgcolor=#FFEEDD><h1>Staff Information
  {% block pagetitle %} Page Name {% endblock %}</h1>
  {% block tagline %} What this is {% endblock %}<br />
  {% block mainsection %} The Real McCoy {% endblock %}
  <hr />
  Copyright {{copyright}} in year {{year}}

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:

  {% extends "staff/mybasepage.html" %}
  {% block pagetitle %}Index{% endblock %}
  {% block tagline %}Here are the various Roles, matey!{% endblock %}
  {% block mainsection %}{{mainbody|safe}}{% endblock %}

You'll note that it's in this file that we reference the overall template shown above, which we had chosen to call mybasepage.html

Create a second template - for the second type of page - here's the template page for each single record:

  {% extends "staff/mybasepage.html" %}
  {% block pagetitle %}[{{role}}]{% endblock %}
  {% block tagline %}Here are you colleagues in that area{% endblock %}
  {% block mainsection %}
  {% for colleague in clist %}
    &bull; How about asking {{colleague.0}}<br />
  {% endfor %}
  <hr />Please select <a href=index.html>EAR</a> for the Eindex
  {% endblock %}

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
Y306 - Python - The Django web framework
  [4095] Django - first steps - Updated - (2013-05-19)
  [4013] Web Frameworks - nested templates - (2013-02-22)
  [3919] What is a web framework? - (2012-11-10)
  [3705] Django Training Courses - UK - (2012-04-23)
  [3698] How to stop forms on other sites submitting to your scripts - (2012-04-15)
  [3640] Sessions (Shopping Carts) in Django - the Python Web Framework - (2012-03-05)
  [3639] Demonstration of a form using Django - (2012-03-04)
  [3634] Defining database relations in your Django model - (2012-03-02)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [3140] Django - separating the HTML from the view / model - (2011-01-20)
  [3139] Steering our Python courses towards wxPython, SQLite and Django - (2011-01-19)
  [3138] Django - adding your own views, and then templating your views. - (2011-01-18)
  [3136] A framework with python - Django - first steps - (2011-01-17)
  [1745] Moodle, Drupal, Django (and Rails) - (2008-08-08)

Back to
What is Make?
Previous and next
Horse's mouth home
Forward to
Defining database relations in your Django model
Some other Articles
April, May and June 2012 - Public Open Source Programming Courses
Best tenner I ever spent?
Parse error: parse error, unexpected T_STRING on brand new web site - why?
Nesting Templates in Django
What is Make?
Getting around Dublin by public transport - some observations
Serialsing and unserialising data for storage and transfer in Perl
Sharing lots of values in Tcl without having lots of global declarations
Historic Drawings from near Melksham
4754 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

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.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3633_Nes ... jango.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat