Accessing global variable in view using context processor in Django
Asked Answered
L

3

8

Assuming I have a context processor:

def title(request):
   return {'titles': 'mytitle'}

I can access this variable in template as {{ titles }}.

But how can I do so in a view?

def myview(request):
    print request.titles

doesn't seem to work - 'WSGIRequest' object has no attribute 'titles'


Or maybe there is a better approach (than context processors) to have global variables accessible in both views and templates?

Thanks in advance.

Laclair answered 15/10, 2011 at 16:38 Comment(0)
M
6

Context processors aren't in any way global variables. They are simply functions that are run when a RequestContext is initiated, which add items to that context. So they're only available wherever you have a RequestContext, ie in a template.

Your examples don't really give a good idea of what variables you're looking to access. If it's just some constants you want to use everywhere, a good way is to define them somewhere central, say in settings.py, and import that module wherever you need it - plus use a context processor to add them to the context.

Mesics answered 15/10, 2011 at 16:49 Comment(1)
Thanks a lot. Defining constans in that way and using context processor to add them to the context is what I was looking for.Laclair
H
10

Add your context processor method path (folder.context_processor.application_context) to TEMPLATE_CONTEXT_PROCESSORS.

in my case application_context is the method which i defined inside file context_processor.py and method "application_context" returns {'titles': 'mytitle'}

if you want to use "title" as global variable in views use it in this way

global_var = RequestContext(request).get("app_config")
titles = global_var.get("titles")
print titles 

The only advantage is "Same variable 'titles' will be visible for templates and also in your views"

Henslowe answered 9/7, 2012 at 14:6 Comment(1)
What is "app_config" in this example?Formic
M
6

Context processors aren't in any way global variables. They are simply functions that are run when a RequestContext is initiated, which add items to that context. So they're only available wherever you have a RequestContext, ie in a template.

Your examples don't really give a good idea of what variables you're looking to access. If it's just some constants you want to use everywhere, a good way is to define them somewhere central, say in settings.py, and import that module wherever you need it - plus use a context processor to add them to the context.

Mesics answered 15/10, 2011 at 16:49 Comment(1)
Thanks a lot. Defining constans in that way and using context processor to add them to the context is what I was looking for.Laclair
C
1

If you need the data in your views, it's cleaner to use Middleware in conjunction with a Context Processor:

  1. Create a trivial custom middleware to store some data on the request object, say at request.x (example). Now you can access this in your views directly.
  2. Enable django.core.context_processors.request in your TEMPLATE_CONTEXT_PROCESSORS to have request.x accessible from your templates.

See my related question: Django: How to provide context to all views (not templates)?

Counts answered 7/5, 2016 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.