DEV Community

Using Context processor in Django to create dynamic footer

Heinek Halwin on February 21, 2020

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