1

What is the mechanics behind context processors in Django?

I have used them before, but if someone asks me to teach them what context processors are about, I probably could not answer. This is from Django documentation:

context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dict of items to be merged into the context.

Can someone elaborate on this. What are context processors exactly ?

scharette
  • 9,437
  • 8
  • 33
  • 67
  • 2
    Templates use context. Context processors add stuff to the context used by templates automatically. – kichik Nov 07 '17 at 17:46
  • If a callable is defined as a context processor and it returns a value (usually a dict) such as: `{ 'foo': 'bar' }`, then all the templates in the project will have `context['foo'] == 'bar'`. – Rob L Nov 07 '17 at 18:06
  • Thanks for your answers ! I feel like it is so much more. Don't you think explanations and examples would be better suited as an answer ? Or I am overthinking this ? because reality is, there is really low documentation on the subject and very few guide on how tom implement this. Maybe I am wrong. – scharette Nov 07 '17 at 18:17
  • 1
    You're overthinking it. It just adds more items to the context. – kichik Nov 07 '17 at 18:37

0 Answers0