How to Load Context Processors in Django Web-based Application

Posted on

This is an article where the main purpose is to show how to load context processors file in the Django web-based application. Basically, context processors file is very useful for having exact same content over and over again in every templates available in the Django web-based application. So, how to add a context processors file in Django web-based application ?, the following is the step to achieve it :

1. Add a file in the pattern of ‘loads/context_processors.py’. The following is a project and an application powered by the Django framework. It is generated by executing certain command :

C:\python-win\app>tree
Folder PATH listing for volume Windows
Volume serial number is xxxxxxxxx
C:.
├───loads
│   ├───management
│   │   └───commands
│   ├───migrations
│   │   └───__pycache__
│   ├───static
│   │   └───loads
│   ├───templates
│   │   ├───loads
│   │   │   ├───admin
│   │   │   ├───emails
│   │   │   ├───external
│   │   │   ├───generators
│   │   │   ├───loads
│   │   │   ├───modules
│   │   │   ├───projects
│   │   │   ├───staff
│   │   │   ├───tasks
│   │   │   └───workpackages
│   │   └───registration
│   ├───templatetags
│   │   └───__pycache__
│   └───__pycache__
└───app
    └───__pycache__
C:\python-win\app>

So, create a file with the name of ‘context_processors.py’ in the load folder :

C:\python-win\app\loads>tree /F
Folder PATH listing for volume Windows
Volume serial number is E003-3593
C:.
│   admin.py
│   context_processors.py
│   decorators.py
│   forms.py
│   helper_functions.py
│   models.py
│   moderator_fix.py
│   tests.py
│   views.py
│   __init__.py
│

2. Fill the file ‘context_processors.py’ any associated contents.

3. After successfully fill the file ‘context_processors.py’ with the suitable content, just add the context_processors.py in the settings file of Django Web-based application. Add the following line in the ‘settings.py’ file as follows :

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        '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',
                'loads.context_processors.variable'
            ],
        },
    },
]

There is a specific line of configuration added in the above configuration setting. That line is :

'loads.context_processors.variable'

It is because the file has the name of ‘context_processors’ and it exists in the ‘loads’ folder. Furthermore there is a ‘variable’ after the ‘context_processors’ part. It is actually indicating the variable name where it is going to be loaded over and over in every templates available in the Django web-based application.

One thought on “How to Load Context Processors in Django Web-based Application

Leave a Reply