What do you think about here lambda function to configure TEMPLATE_DIRS with relatives values ?

45 views
Skip to first unread message

klein.s...@gmail.com

unread,
Jun 30, 2009, 7:38:32 AM6/30/09
to Django users
Hi,

When I configuration my TEMPLATE_DIRS in settings.py file, I use ever
"here" or
"here_cross" lambda function defined like this :

::

import os
here = lambda x: os.path.join(os.path.abspath(os.path.dirname
(__file__)), x)
here_cross = lambda x: os.path.join(os.path.abspath(os.path.dirname
(__file__)), *x)

I can configure TEMPLATE_DIRS or MEDIA_ROOT like this :

::

MEDIA_ROOT = ''


TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/
www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
here('myapp/templates'),
here_cross(('otherapp', 'templates')),
)

I suppose that this comment "Don't forget to use absolute paths, not
relative paths" is
relevant if I don't use "here" or "here_cross" function. If I don't
use it,
template paths are relative to current working directory and not to
"settings.py" file.

What do you think about this method (using here) ?
Why don't put "here" or/and "here_cross" in Django settings.py
skeleton ?

Note, I found here and here_cross tip on this page :
http://www.djangosnippets.org/snippets/445/

Thanks for your comment.
Regards,
Stephane

KLEIN Stéphane

unread,
Jun 30, 2009, 7:54:41 AM6/30/09
to Django users
Sorry, I've some break line issue in my previous post.
This is my post cleaned.

Hi,

When I configuration my TEMPLATE_DIRS in settings.py file, I use ever
"here" or "here_cross" lambda function defined like this :

::

import os
here = lambda x: os.path.join(os.path.abspath(os.path.dirname
(__file__)), x)
here_cross = lambda x: os.path.join(os.path.abspath(os.path.dirname
(__file__)), *x)

I can configure TEMPLATE_DIRS or MEDIA_ROOT like this :

::

MEDIA_ROOT = ''


TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/
www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
here('myapp/templates'),
here_cross(('otherapp', 'templates')),
)

I suppose that this comment "Don't forget to use absolute paths, not
relative paths" is relevant if I don't use "here" or "here_cross"
function. If I don't use it, template paths are relative to current
working directory and not to "settings.py" file.

What do you think about this methode (using here) ?

Michael

unread,
Jun 30, 2009, 9:27:26 AM6/30/09
to django...@googlegroups.com
If it works for you, it works. Personally I find lambdas a little difficult to read, and personally in this situation, I just put:
os.path.normpath(os.path.join(os.path.dirname(__file__), 'otherapp', 'templates'))

inside of TEMPLATE_DIRS directly. I find that clearer to see what is happening, it is all in one line and still really simple.

But what works for you works for you.

Michael 

Darryl Ross

unread,
Jun 30, 2009, 11:28:04 AM6/30/09
to django...@googlegroups.com
Hi Stephane,

I think it is a great idea. The lambda trick means I don't have to make
any changes at all to switch between my development environment and
production. A real life example from a current project:

-----

from os.path import abspath, dirname, join
APP_PATH = lambda *x: abspath(join(dirname(__file__), *x))

MEDIA_ROOT = APP_PATH('webroot', 'media')
TEMPLATE_DIRS = (
APP_PATH('templates'),
)

-----

Note the * on the definition for x which means you only need one lambda.

On my production machines, the websites typically run from

/home/httpd/<domain>

but on my development machine (my laptop), they run from

/home/darryl/devel/websites/<customer>/<domain>

Cheers
Darryl


signature.asc
Reply all
Reply to author
Forward
0 new messages