September 12, 2007

One thing I absolutely love about Django’s template loading

Is the abil­ity to spec­ify a series of tem­plate fold­ers in your set­tings file.

Here’s what we do at work:

1
2
3
4
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), "templates"),
    os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "ANOTHERSITE", "templates")),
)

It allows us to easily share tem­plates from ANOTH­ER­SITE with sister/sub sites, but when/if you need to over­ride them you just drop your new tem­plate your site’s tem­plates folder and you’re off to the races.

Another template tip

If you use the tip above, I’d rec­om­mend putting tem­plates that are explic­ity shared in a templates/shared folder.

Then you can do things like:

1
2
<!-- filename: templates/base_somethingorother.html -->
{% extends 'shared/shared_somethingorother.html' %}

It’s a minor tip, but it helps pre­vent col­li­sions and alerts devel­op­ers that these tem­plates shouldn’t have any site spe­cific code in them.

Filed under: Django,Python

Next:
Previous:

Related