Saturday, May 28, 2011

django template loading

Templates are a really a great feature, using it is a delight as it lets the python developer concentrate on the backend code letting the designers concentrate on the page design.

If you are developing on a local dev machine and uploading it to a live server, template path can be an issue of concern for you.

To make it path independent there is a thing you can do to bypass that issue.
In the settings file, look for TEMPLATE_DIRS here you can use the python magic variable to chalk out a relative path.

import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\', '/'),
)

__file__ variable points to the current path and the templates directory can be joined on to the path.
In practical scenario __file__ will contain the src folder, so keep the templates folder inside the src folder.

Isn't it neat.

Regards,
Ishan

No comments:

Post a Comment