6

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.

Banjer
  • 8,118
  • 5
  • 46
  • 61
Chris Muench
  • 17,444
  • 70
  • 209
  • 362

1 Answers1

8

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.

girasquid
  • 15,121
  • 2
  • 48
  • 58