Templating in Django
I've been trying to make a template layout for Django that makes sense, something similar to existing PHP frameworks, where the head section is in one file, and the body is in another.
To do that I simply created a file called head.html (under templates/ obviously), and included it:
head.html
|
|
Now using the Django templating language, we simply include the file:
index.html
|
|
This seems fine for starters, but what if we want to change the content (outrageous isn't it)?
In this case, we could use extend (on that specific page), which is a very powerful tool to get some generic layouts and always have the ability to override them, in this case we just override the content by redeclaring the block:
special_page.html
|
|
There are different ways for creating these layouts on Django, though not many examples are given (at least not without digging in github).
If we want to build a website with a lot of pages, most of them none-static (stored on the Database, Flatpages is a very helpful package (application?) for that.
It allows you to store pages on the database, and have a default layout for them (which you can customize).
Another helpful package along that line would be Django-chunks which works just like blocks for those of you familiar with popular CMS's
I've noticed that the best approach (for me at least) is to implement layouts on my own,
That way, I have to learn more about a core feature (templating and rendering) of Django instead of learning some third party package,
not to mention nothing is more familiar than your own code (hopefully).
Update
I highly recommend using django-apptemplates,
it's very helpful when extending and including template names that are the same throughout different applications.
To implement it you have to add the library to the TEMPLATE_LOADERS in the settings file,
and then you simply write %7B%25 extends "appname:index.html" %25%7D (adding the appname), same goes for include.