DEV Community

Heinek Halwin
Heinek Halwin

Posted on

Using Context processor in Django to create dynamic footer

Intro to Context Processor

Hi all,

We can all agree that we have come across request.user in a django project. Have you ever wondered what it was ? Well, its an inbuilt context processor.

A context processor is a function that accepts an argument and returns a dictionary as its output. In our case, the returning dictionary is added as the context and the biggest advantage is that, it can be accessed globally i.e, across all templates. While this might seem too complex or hard to achieve, its actually not the case and it is very simple to implement.

So, today we will look into building a dynamic footer using the context processor - a footer which can render the year dynamically, pretty convenient, when you dont have to change your source code every year. I'm assuming you have a django project and an app already built.

Building a dynamic footer using context processor

First, you need to create a file named context_processors.py. Here we will add the following code.

import datetime

def get_current_year_to_context(request):
    current_datetime = datetime.datetime.now()
    return {
        'current_year': current_datetime.year
    }

Second, you need to specify your context processor in your settings file.

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.get_current_year_to_context', # <-- Add your context processor
            ],
        },
    },
]

Thats it. Your context processor is now ready to be used in your templates and it can be accessed using the key that we specified 'current_year'. So, to create a dynamic footer section, all i need to do is.

    Copyright {{ current_year }} - Heinek Halwin

Run your project and you can see that the year is now visible as the current year. Have fun using this in your projects.

Have a great day !

Top comments (15)

Collapse
 
minaee profile image
minaee

I did exactly the same procidure, but I want to pass 4 images to the navbar of my website. I am accessing the image as following:
<img src="{{ labels.label_services.url }}">

but it doesnt work! any ideas?

Collapse
 
harveyhalwin profile image
Heinek Halwin

Did you setup the static and media urls in settings ? It should work if you had set those up.

Collapse
 
minaee profile image
minaee

the problem was with the looping through the dict that I sent in the template. your explanations were sufficient. thanks

Collapse
 
tombohub profile image
tombohub

thanks, very simple and nice.

Does using a lot of context processors create a mess? How do you keep tracking which one is context processor which one is normal?

Collapse
 
harveyhalwin profile image
Heinek Halwin

I did switch over to using React/Next.js as my frontend recently. 😁

But, i would suggest using a naming scheme and folder structure, so you know where and which ones are yours.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
harveyhalwin profile image
Heinek Halwin

Hi, What is the problem ?

Collapse
 
anilbhattaraitoronto profile image
Anil Bhattarai

I am trying to say that there is simpler way to write year in footer in Django without resorting to creating forms. But the liquid language that Dev.to uses does not allow me to insert it

Thread Thread
 
harveyhalwin profile image
Heinek Halwin

The footer is just an example of using context processor. But I am interested to know an alternate method. Can you write it elsewhere and send me the link. I’ll add it to the blog.

Thread Thread
 
anilbhattaraitoronto profile image
Anil Bhattarai

{percent now 'Y' percent}
replace 'percent' with %
the now method with 'Y' returns the given year in the template

Collapse
 
markmichon1 profile image
Mark Michon

This was exactly what I needed. Thank you!

Collapse
 
harveyhalwin profile image
Heinek Halwin

Happy to help.

Collapse
 
anilbhattaraitoronto profile image
Anil Bhattarai

{percent now 'Y' percent}
replace the 'percent' with %

Collapse
 
ajinkabeer profile image
Ajin Kabeer

Awesome!

Collapse
 
harveyhalwin profile image
Heinek Halwin

Thanks man