python - Template directory logic in Django -
i've been fooling around templates time now, , loving every moment of django experience. however, since django such big fan loose coupling, wanted know, why not have piece of code:
import os import platform if platform.system() == 'windows': templatefiles = os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/') else: templatefiles = os.path.join(os.path.dirname(__file__), '..', 'templates') template_dirs = ( # includes templates folder templatefiles, )
instead of:
import os template_dirs = ( templatefiles = os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/') )
would not first example follow philosophy of loose coupling better second (which believe does), , if so, why django default second code example , not first?
you ask, "why django default second code example?" in django 1.5, when run
$ django-admin.py startproject mysite
i find settings.py
contains:
template_dirs = ( # put strings here, "/home/html/django_templates" or "c:/www/django/templates". # use forward slashes, on windows. # don't forget use absolute paths, not relative paths. )
so not sure example code coming from: it's not django's default.
on non-windows systems rare find backslashes in directory names, second example work in practical cases. if had bullet-proof write:
import os base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) template_dir = os.path.join(base_dir, 'templates') if os.sep != '/': # django says, "always use forward slashes, on windows." template_dir = template_dir.replace(os.sep, '/') template_dirs = (template_dir,)
Comments
Post a Comment