Django context processors for global template variables

657
0

Context processors add variables to every template context automatically. I create a function that takes request and returns a dict. This is perfect for site-wide settings like site name, current year, or user preferences. I add my processor to TEMPLATES['OPTIONS']['context_processors'] in settings. Unlike middleware, processors only run when rendering templates. I keep processors lightweight since they run on every template render. For expensive data, I use caching inside the processor. This reduces duplication across views that all need the same context variables.