TEMPLATE_DIRS = ('/path/to/templates/',)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
I'm trying to find a solution that would list the contents of my specified directory in either of these locations (TEMPLATE_DIRS
or TEMPLATE_LOADERS
).
I need something like:
template_files = []
for dir in EVERY_DIRECTORY_DJANGO_LOOKS_FOR_TEMPLATES_IN:
template_files.append(os.listdir(dir))
To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.
The default templates used by the Django admin are located under the /django/contrib/admin/templates/ directory of your Django installation inside your operating system's or virtual env Python environment (e.g. <virtual_env_directory>/lib/python3. 5/site-packages/django/contrib/admin/templates/ ).
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
In case anyone is still needing this, I'm running 1.9.2 and it looks like
app_template_dirs
is now get_app_template_dirs
and settings.TEMPLATE_DIRS
is now settings.TEMPLATES[0]['DIRS']
Here's what I did:
from django.conf import settings
from django.template.loaders.app_directories import get_app_template_dirs
import os
template_dir_list = []
for template_dir in get_app_template_dirs('templates'):
if settings.ROOT_DIR in template_dir:
template_dir_list.append(template_dir)
template_list = []
for template_dir in (template_dir_list + settings.TEMPLATES[0]['DIRS']):
for base_dir, dirnames, filenames in os.walk(template_dir):
for filename in filenames:
template_list.append(os.path.join(base_dir, filename))
Then you can iterate through the list as you need using template_list:
for template in template_list:
print template
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With