Tuesday, October 28, 2014

Re: Generating a list of available templates

I know this thread has been dead for over 2 years, but I've been trying to solve this problem or a similar problem today for a library I'm working on.

I've only tested it with the filesystem and app_directories loaders, but the code I've got to so far looks like this:

for loader in settings.TEMPLATE_LOADERS:
    loader_module = importlib.import_module('.'.join(loader.split('.')[:-1]))
    print '\n'.join(source for source in loader_module.Loader().get_template_sources(''))

Just thought I'd share it in case anyone was interested.

Cheers,


William

On Saturday, February 11, 2012 5:30:50 PM UTC+2, pw wrote:
Thanks for weighing in, that is helpful, and last night I was playing around with a somewhat similar approach. It's not exactly what I was hoping for, because it still isn't really integrated with the way Django uses template loaders.

This solution works for TEMPLATE_DIRS and INSTALLED_APPS, but there are other possible template loaders, or a user might have written a custom template loader. Or they may have disabled the app directories loader… Anyway, you get the idea.

If the built-in loader classes had some method that would just return the paths to the template directories that were loaded, this could work perfectly, but I think the loaders can only check to see if a supplied template name exists on one of the paths they serve. So it seems like my idea is not really feasible in a way that integrates cleanly with Django and results in a portable application. Thanks to everyone who replied with assistance.

- Patrick

On Saturday, February 11, 2012 at 5:39 AM, bb6xt wrote:

Here's my two cents. Hope it helps some.

from proj.settings import TEMPLATE_DIRS, INSTALLED_APPS
import os

def recursive_search(path):
result = []
for i in os.listdir(path):
if not os.path.isdir(os.path.join(path, i)):
result.append(i)
else:
result.extend(recursive_search(os.path.join(path, i)))
return result

def myview(request):
templates = []
# the next 2 lines handles templates in explicit template
directories
for path in TEMPLATE_DIRS:
templates.extend(recursive_search(path))
# the next 2 will handle template dir generated py the
appdirectories loader excluding contrib apps
for app in INSTALLED_APPS:
if app.startswith('proj'):
if os.path.exists(os.path.join(ABSPATH_TO_PROJ,
app.split('.')[-1], 'templates')):
templates.extend(recursive_search(os.path.join(ABSPATH_TO_PROJ,
app.split('.')[-1], 'templates')))

well, you have a list of templates with which you can do as you
please. surely you'll want to keep track of the actual path django
uses to locate these templates, you can do so by tweaking the
recursive_search funct a little

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/92d8ba74-700a-420e-8c12-10140e1c7073%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment