1 min read

Django: Configure template rendering

Django: Configure template rendering
Photo by Fahrul Razi / Unsplash

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.

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.py

import 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.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        
        'DIRS': [os.path.join(BASE_DIR, 'templates')], # - 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',
            ],
        },
    },
]