Opened 8 years ago

Closed 8 years ago

#27570 closed New feature (wontfix)

Make context processor run outside of RequestContext

Reported by: Anthony King Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Not all templates are generated within the context of a request, such as email rendering.
This prevents you accessing variables that are set in your context processors.

In our codebase, we pass in some settings and other constants via a context processor, which never looks at the request object.

It would be convenient for those constants to be used in all templates, as apposed to templates with requests only.

I see 2 possible approaches to adding this functionality:

  • Add a constant_processor, however this wrongly limits it to constants.
  • make the request argument optional going forward, and use arg inspections to conditionally context processors for a period.

Using some_context_processor(request=None) would show the processor supports request objects not being set.

Change History (3)

comment:1 by Aymeric Augustin, 8 years ago

Quoting myself or #django-dev:

I would be more happy to support an option to add globals to Django templates — that could be a simple configuration option — than to start executing request context processors without a request

for your own projet, you can create a custom template backend that inherits the Django template backend, overrides render, and adds your globals to the context before calling the parent

comment:2 by Tim Graham, 8 years ago

Is adding another way to do this better than using a template tag? You could add the tag to TEMPLATES['OPTIONS']['builtins'] to avoid {% load ... %} in the template.

comment:3 by Tim Graham, 8 years ago

Resolution: wontfix
Status: newclosed

Feel free to reopen with an explanation of why the solution I proposed isn't sufficient.

Note: See TracTickets for help on using tickets.
Back to Top