Django: Configure template rendering
How to configure Django to connect to our templates directory
What are templates?
Django templates are text files, which can be composed to run as HTML files — which can later be referred to as HTML templates, once templating has been configured in Django.
So, let’s get started
…
Step 1:
Make sure that you create a templates folder in your root/base directory, like so:
# - Base/Root directory MyProject
---> MyProject(contains settings.py)
---> MyApp
---> static
---> templates
---> venv
---> db.sqlite3
---> manage.py
Step 2:
Import the os module at the top of your settings.py file:
# settings.pyimport os
Step 3:
With the os module, you need to tell Django where to look for our templates. This needs to be configured in DIRS, under our template configuration settings:
# settings.pyTEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # - Configure DIRS
'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', ],
},
},
]
A final note…
For those that are interested in learning Django from scratch, feel free to check out my latest course:
Python Django: Ultimate Beginners Course — 2022
->
Originally published at https://www.cloudwithdjango on August 7, 2022.