Created
October 20, 2021 14:55
-
-
Save bmispelon/b02c27d3cdd05e797818cb123faf68bd to your computer and use it in GitHub Desktop.
Exhaustively list all available templates in a Django project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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