Learn Django

Configure Template Settings

In order for Django to serve templates we need to configure a few things. The first thing to create is a folder where template files (e.g. HTML files) can be saved. This folder is created in the project directory. Once the directory is created, the settings.py file must be updated to point to it. We also want to enable an additional context processor while we're making template related changes. Once these tasks are completed Django will be able to render template files.

Step 1: Create the Templates Folder

In this step we'll create the templates directory.

  1. In the /.../crmeasy/crmapp folder create a new folder named templates/
  2. That's it

Step 2: Tell Django Where the Templates Folder Is

Now it's time to update the settings.py file with the location of the templates folder. Follow these steps to do so.

  1. Open the /.../crmeasy/crmapp/settings.py file
  2. Add this code to the end of the file
1
2
3
4
# Template directory setting
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates'),
)

Step 3: Modify Template Context Processors

Django uses context processors when processing web requests. There are several context processors enabled by default. However, an additional processor are needed to enable parts of functionality in this course.

Add the following code to the end of the settings.py file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Custom template context processor setting
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    'django.core.context_processors.request',
)

Step 4: Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "set template settings"

Track your progress with a free account