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 :-)