I have a bunch of variables that need to be available to the view for all templates. It seems the best choice would be a context processor.
The documentation says:
A context processor has a very simple interface: It's just a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.
If I need to do more advanced lookups, can I define other functions? Do the functions need to be in a class? I was thinking of creating a file named context_processors.py
in my app folder.
You can define other functions, and the functions don't need to be in a class.
Typically people put their context processors into a context_processors.py
like you're thinking of as functions, and then name them all in settings.TEMPLATE_CONTEXT_PROCESSORS
.
For example, here's an app that has the context_processors.py
inside it: django-seo .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.