Django is a global variable. Should I use a template context handler?

my question is: Should I use a template context handler for a global variable, such as a list of categories?

I have globs.py

from news.models import Category def globs(request): cats = Category.objects.all() return {'cats': cats} 

and in settings.py

 TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "globs.globs",) 

And when I use "cats" in the template, it works fine on the development server.

I have a problem on my hosting: Error importing arrays of request processor modules: "There is no module named globs"

Can I use something else for global variables?

+4
source share
1 answer

globs.py should be in your imported path on your hosting server. You can transfer it to your news directory and use "news.globs.globs" in TEMPLATE_CONTEXT_PROCESSORS.

+5
source

Source: https://habr.com/ru/post/1340694/


All Articles