'Django context processors available globally to templates in ALL apps
I am new to Django/Python, and am trying to figure out the best way to have global constants in my project that are available to templates in ALL APPS in the project. Basic globals like Company name, Phone number, url, social links, etc.
After quite a bit of searching, the best answer I have found is to create custom context processors, but the way I am understanding it would require me to create a custom content_processors.py file in each app, and add a reference to each one in the main project’s settings.py. This seems to violate the DRY concept, and I am assuming I must be missing something basic.
So here is what I have now. My project is called www and one of the apps is called home.
In www/settings.py I have a line as follows:
COMPANY_NAME = ‘My Company Name’
I created this file home/context_processors.py with the following contents:
from django.conf import settings
def www(request):
return {
'company_name': settings.COMPANY_NAME
}
In the TEMPLATES/OPTIONS/content_processors section of my www/settings.py, I added:
’home.context_processors.www’
And my homepage template in /home/templates has
{{ company_name }}
This works perfectly, but now when I create another app called products, it seems as though I need to add another file of products/context_processors.py with the exact same content as home/context_processors.py, and I need to add another line in settings for ’products.context_processors.www’
Of course, I can do this, but it seems like quite a bit of repetition, especially given that I will have dozens of apps for this site. I assume there must be a way to make these common strings (and other settings) available to all templates in all apps globally.
Hoping someone points out a simple error in my understanding :-)
Solution 1:[1]
I was able to finally get an answer in the Unofficial Django Discord Forum, so I thought I would post the solution here in case others run into the same problem.
It turns out that by setting the context_processors in just one app, anything used by it becomes available globally. So based on my above example:
(1) In the project's settings.py, I add in the context_processors section:
'home.context_processors.site_settings'
(2) I add toward the end of settings.py:
COMPANY_NAME = ‘My Company Name’
(3) Considering my first app in the project is called home, I add a folder in that app's directory called contenxt_processors.py, and add the following into the file:
from django.conf import settings
def site_settings(request):
return {
'company_name': settings.COMPANY_NAME,
}
(4) Now I have the constant available to me in all templates, in all of my apps, without having to repeat the process in other apps. It seems counter-intuitive, but it works like a charm. So in any template that I add {{ company_name }}, it will display My Company Name, as I set it in the settings.py file.
(5) Now if I want to add other constants, I simply add them to the settings.py file under COMPANY_NAME = ‘My Company Name’. For example, I may now add
COMPANY_URL = 'www.mydomain.com'
and then I also add it to the context_processors.py file in my primary app folder. Such that it now looks like this:
from django.conf import settings
def site_settings(request):
return {
'company_name': settings.COMPANY_NAME,
'company_url': settings.COMPANY_URL,
}
That was it, and now I have {{ company_name }} and {{ company_url }} available in all of my templates
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mck |
