Adding and removing template dirs on the fly

10 views
Skip to first unread message

3xM

unread,
Jul 17, 2008, 7:23:55 AM7/17/08
to Django users
Is there a "right" way to add and remove directories to TEMPLATE_DIRS
on the fly? Is it right to do at all?

I tried to just add a directory, but realized that the settings are
not per request, but per server thread, so that a directory added by
one request is also there for the next request, which causes some
trouble for me.

My case:

I am making a generic system for generating some playlists. Playlists
for most channels look exactly the same, but some has small
differences in how it should appear (e.g. they should not include a
thumbnail for the video, or maybe the order of the data presented
should be slightly different).

I'm using a set of templates, each one for each part of the playlist,
making it easy - in theory - to alter small parts of the playlist for
each channel, simply by adding another template directory for the
specific channel.

But when the added directory is stored across requests, a playlist for
Channel A might be generated with templates from Channel B if the
template directory for the latter has been added to TEMPLATE_DIRS.

My solution for now is adding the custom directory (if necessary), and
then removing it at the end of the request, after the HTML has been
generated.

BUT! What happens then if 2 request hit the same server thread at the
same time? Is there a risk of the second request getting the wrong
templates (from the first request) if the first request has not yet
removed it's directory from TEMPLATE_DIRS? Or are requests to the same
thread queued and won't interfere with each other?

--

Best regards, Mikkel

Arien

unread,
Jul 17, 2008, 7:59:32 AM7/17/08
to django...@googlegroups.com
On Thu, Jul 17, 2008 at 6:23 AM, 3xM <3...@detfalskested.dk> wrote:
[...]

> I am making a generic system for generating some playlists. Playlists
> for most channels look exactly the same, but some has small
> differences in how it should appear (e.g. they should not include a
> thumbnail for the video, or maybe the order of the data presented
> should be slightly different).
>
> I'm using a set of templates, each one for each part of the playlist,
> making it easy - in theory - to alter small parts of the playlist for
> each channel, simply by adding another template directory for the
> specific channel.

Have you seen the section on loading templates in the documentation?
It contains a tip that describes how you can use select_template to
nicely deal with this type of situation:
http://www.djangoproject.com/documentation/templates_python/#loading-templates


Arien

3xM

unread,
Jul 17, 2008, 9:04:54 AM7/17/08
to Django users


On Jul 17, 1:59 pm, Arien <regex...@gmail.com> wrote:

> Have you seen the section on loading templates in the documentation?
> It contains a tip that describes how you can use select_template to
> nicely deal with this type of situation

Thanks for the fast reply.

I Don't see how this would help me. I still want to rely on the
fallback features of the template loading from directories in
TEMPLATE_DIRS, as I don't want to copy templates for all parts of the
playlist for each channel that differs a bit, but only those parts
being different.

I already tried loading templates a la

'%s/name_of_template.html' % channel_template_folder

but the result is that it when the template includes other templates,
it will look for it in the same folder, not doing the fallback thing
to the main generic template folder (as it is looking for something in
a subfolder with the name of the value of channel_template_folder).

Does it make sense?

I'll try to illustrate the current template structure to clarify:

.../templates
part_1.html
part_2.html
/channel_a
part_1.html
/channel_b
part_2.html

(I hope the identation in the above doesn't get ignored as whitespace)

So, normally I load e.g. 'part_1.html' which, somewhere in it,
includes 'part_2.html'. That works just fine.

But for Channel A I used to load 'channel_a/part_1.html', which will
also include 'part_2.html'. But now the template system will look for
a file called 'channel_a/part_2.html' which doesn't exist. And for
Channel B, where I only want 'part_2.html' to be different from the
generic, I can't get it to work without adding 'templates/channel_b'
to TEMPLATE_DIRS. A "solution" is to copy all templates each time I
want to make differences, but that seems quite stupid to me.

So what I want is to temporarily add 'templates/channel_b' to
TEMPLATE_DIRS for requests for playlists from Channel B. Is it somehow
possible?

Arien

unread,
Jul 18, 2008, 6:28:38 PM7/18/08
to django...@googlegroups.com
On Thu, Jul 17, 2008 at 8:04 AM, 3xM <3...@detfalskested.dk> wrote:
> I already tried loading templates a la
>
> '%s/name_of_template.html' % channel_template_folder
>
> but the result is that it when the template includes other templates,
> it will look for it in the same folder, not doing the fallback thing
> to the main generic template folder (as it is looking for something in
> a subfolder with the name of the value of channel_template_folder).
>
> Does it make sense?

Right, the includes will be relative to TEMPLATE_DIRS.

> I'll try to illustrate the current template structure to clarify:
>
> .../templates
> part_1.html
> part_2.html
> /channel_a
> part_1.html
> /channel_b
> part_2.html
>
> (I hope the identation in the above doesn't get ignored as whitespace)
>
> So, normally I load e.g. 'part_1.html' which, somewhere in it,
> includes 'part_2.html'. That works just fine.
>
> But for Channel A I used to load 'channel_a/part_1.html', which will
> also include 'part_2.html'. But now the template system will look for
> a file called 'channel_a/part_2.html' which doesn't exist.

The template system will only look for "part_2.html" under "channel_a"
if "channel_a" is in your TEMPLATE_DIRS.

> And for Channel B, where I only want 'part_2.html' to be different
> from the generic, I can't get it to work without adding
> 'templates/channel_b' to TEMPLATE_DIRS. A "solution" is to copy all
> templates each time I want to make differences, but that seems quite
> stupid to me.

Or you could leave TEMPLATE_DIRS alone and use this in "channel_b/part_1.html":

{% include "channel_b/part_2.html" %}

> So what I want is to temporarily add 'templates/channel_b' to
> TEMPLATE_DIRS for requests for playlists from Channel B. Is it somehow
> possible?

But is it a good idea?

If it turns out that the only solution for your problem is to have
some fallback mechanism when including templates, you could always
write a template tag to do just that. And maybe you can solve it
using some combination of template inheritance, inclusion and
select_template.


Arien

Martin Diers

unread,
Jul 20, 2008, 8:04:41 AM7/20/08
to django...@googlegroups.com
Couldn't you just create a dummy file in /channel_a for part2.html,
and in /channel_b for part1.html, such that the dummy files do nothing
but include the content of the corresponding parent?
Reply all
Reply to author
Forward
0 new messages