What is the best location to put templates in django project?
You could also consider having your templates in a database, using django-dbtemplates. It is also setup for caching, and the django-reversion application which helps you keep old versions of your templates around.
It works quite well, but I'd prefer a little more flexibility on the import/sync to/from filesystem side.
[edit: 20 Aug 2018 - this repository is no available, one with the same name is available at https://github.com/jazzband/django-dbtemplates and was updated 8 months ago. I no longer use Django in any meaningful way, so can't vouch for this.]
From the Django book, chapter 4:
If you can’t think of an obvious place to put your templates, we recommend creating a templates directory within your Django project (i.e., within the mysite directory you created in Chapter 2, if you’ve been following along with our examples).
This is exactly what I do, and has worked great for me.
My directory structure looks something like this:
/media
for all my CSS/JS/images etc
/templates
for my templates
/projectname
for the main project code (i.e. the Python code)
Django 1.10
TEMPLATE_DIRS
is deprecated.
Now we need to use TEMPLATE
, introducing in Django 1.8 like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
},
},
]
Once you have defined TEMPLATES, you can safely remove ALLOWED_INCLUDE_ROOTS, TEMPLATE_CONTEXT_PROCESSORS, TEMPLATE_DEBUG, TEMPLATE_DIRS, TEMPLATE_LOADERS, and TEMPLATE_STRING_IF_INVALID.
About the best location, Django looking for template like this :
More information : https://docs.djangoproject.com/en/1.10/topics/templates/#configuration
Following up from Dominic and dlrust,
We use a setuptools source distribution (sdist) to package our django project and apps to deploy in our different environments.
We have found that the templates and static files need to be under the django application directories so that they can be packaged up by setuptools.
For example, our template and static paths look like:
PROJECT/APP/templates/APP/template.html
PROJECT/APP/static/APP/my.js
For this to work, the MANIFEST.in needs to be modified (see http://docs.python.org/distutils/sourcedist.html#the-manifest-in-template)
An example of the MANIFEST.in:
include setup.py
recursive-include PROJECT *.txt *.html *.js
recursive-include PROJECT *.css *.js *.png *.gif *.bmp *.ico *.jpg *.jpeg
Also, you need to confirm in your django settings file that the app_directories loader is in your TEMPLATE_LOADERS. I think it's there by default in django 1.4.
An example of the django settings template loaders:
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
Just in case you are wondering why we use sdists instead of just coping rsync files; it's part of our configuration management workflow where we have a single build tarball that is deployed with PIP unchanged into test, acceptance and production environments.
DJANGO 1.11
add templates folder where the manage.py exist,which is your base directory. change the DIRS for TEMPLATES as following in your settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Now to use the template by using the code ,
def home(request):
return render(request,"index.html",{})
in views.py. this works completely fine for django 1.11
This is more a personal choice at the project-level. If you are talking about apps that need to be pluggable, then a templates directory in your app is the place where they go default. But project-wide, it is what works best for you.