Django

Introduction to context processor in dajngo

blog image

A context processor is a function that takes a request object as their argument and returns a dictionary to be merged into the context, and it is accessible in all the templates.

Django provides many default context processors that you can see in settings.py file in the default template engine.

 

Custome context processor

You can create your own custom context processors. For that you need to create a file named context_processors.py.

context_processor.py

import datetime

def date_context_processor(request):
    current_datetime = datetime.datetime.now()
    return {
        'current_day': current_datetime.day,
        'current_month': current_datetime.month,
        'current_year': current_datetime.year
    }

In this example we will return current day, month and year, but you can return what ever you want for example you can return query set or current git branch, etc.

Now you need to specify your context_processors in settings file.

settings.py

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',
                'myapp.context_processors.date_context_processor'  # <-- custom context processor
            ],
        },
    },
]

To render this context in your template using current_daycurrent_month and current_year keys.

base.html

{{ current_day }}
{{ current_month }}
{{ current_year }}

 

Show git active barnch 

import datetime
import git
from django.conf import settings

def date_context_processor(request):
    current_datetime = datetime.datetime.now()
    repo = git.Repo(settings.BASE_DIR)

    return {
        'current_year': current_datetime.year,
        'active_branch': repo.active_branch.name
    }

 

 

 


About author

author image

Amrit Panta

Python developer, content writer



3 Comments

Amanda Martines 5 days ago

Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.

Reply

Baltej Singh 5 days ago

Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.

Reply

Marie Johnson 5 days ago

Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.

Reply

Leave a Reply

Scroll to Top