Skip to content

Instantly share code, notes, and snippets.

@bmispelon
Created October 20, 2021 14:55
Show Gist options
  • Save bmispelon/b02c27d3cdd05e797818cb123faf68bd to your computer and use it in GitHub Desktop.
Save bmispelon/b02c27d3cdd05e797818cb123faf68bd to your computer and use it in GitHub Desktop.
Exhaustively list all available templates in a Django project
from pathlib import Path
from django import template
def gen_all_templates():
"""
Generate the list of all available templates as tuples of 3 values:
- the template engine
- the filesystem directory (Path object) where that template is located
- the name of the template
"""
for engine in template.engines.all():
for directory in engine.template_dirs:
if not isinstance(directory, Path):
directory = Path(directory)
for path in directory.rglob('*'):
if not p.is_file():
continue
yield engine, directory, path.relative_to(directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment