Passing variables from a context processor to a template

92 views
Skip to first unread message

Guy Nesher

unread,
Jan 5, 2012, 12:03:56 PM1/5/12
to Django users
Hi,

I've created a simple context processor which simply returns a
variable, however I'm unable to retrieve it from my template.

The context processor is quite simple :

def swiss_context_processors(request):
mytest = "aaa"
return mytest

and I am calling the context processor in my view :

def index(request):
return render_to_response('front/
index.html',context_instance=RequestContext(request)
)

However when I try to access the variable in my template {{mytest}} it
returns empty.

What am I missing ?

Michael Elkins

unread,
Jan 5, 2012, 12:21:41 PM1/5/12
to django...@googlegroups.com
On Thu, Jan 05, 2012 at 09:03:56AM -0800, Guy Nesher wrote:
>I've created a simple context processor which simply returns a
>variable, however I'm unable to retrieve it from my template.

"Each context processor must return a dictionary."

https://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors

Nan

unread,
Jan 5, 2012, 12:22:13 PM1/5/12
to Django users

Two things:

1) make sure the context processor is installed in your settings file.
2) context processors should return dicts. Try:

def swiss_context_processors(request):
added_context = { 'mytest': 'aaa', }
return added_context

Guy Nesher

unread,
Jan 5, 2012, 12:35:05 PM1/5/12
to Django users
Thanks,

I've initially tried to use a dictionary but was still unable to pull
the data in the template.

I'm using your updated context processor:
def swiss_context_processors(request):
added_context = { 'mytest': 'aaa', }
return added_context

and trying to call {{added_context.mytest}} in the template which
returns nothing

I've added the context processor to settings.py and when I run a
pdb.trace() from the context_processor I am able to print the variable
so it is being called + defined.

Rainy

unread,
Jan 5, 2012, 12:38:14 PM1/5/12
to Django users


On Jan 5, 12:35 pm, Guy Nesher <nesher....@gmail.com> wrote:
> Thanks,
>
> I've initially tried to use a dictionary but was still unable to pull
> the data in the template.
>
> I'm using your updated context processor:
> def swiss_context_processors(request):
>     added_context = { 'mytest': 'aaa', }
>     return added_context
>
> and trying to call {{added_context.mytest}} in the template which
> returns nothing


It should be just {{ mytest }}

Also note that a comma at the end is not necessary:

return {'mytest': 'aaa'} or even return dict(mytest='aaa')

-ak

Rainy

unread,
Jan 5, 2012, 12:44:11 PM1/5/12
to Django users


On Jan 5, 12:38 pm, Rainy <andrei....@gmail.com> wrote:
> On Jan 5, 12:35 pm, Guy Nesher <nesher....@gmail.com> wrote:
>
> > Thanks,
>
> > I've initially tried to use a dictionary but was still unable to pull
> > the data in the template.
>
> > I'm using your updated context processor:
> > def swiss_context_processors(request):
> >     added_context = { 'mytest': 'aaa', }
> >     return added_context
>
> > and trying to call {{added_context.mytest}} in the template which
> > returns nothing
>
> It should be just {{ mytest }}


I should add that in Python, it doesn't matter what is the
variable name of value being returned. So if you have

def x(): a=1; return a

def y(): b=x()

There is no way for y() to know that inside x(), the
variable was called 'a'. It receives the value only. It's
effectively equivalent to x() being:

def x(): return 1

So you need to understand that django template
processing receives the dictionary {'mytest':'aaa'} and
could not possibly know what you mean by 'added_context'.

-ak

Guy Nesher

unread,
Jan 6, 2012, 4:34:48 AM1/6/12
to Django users
Thanks,

Works perfectly now (and yeah I'm fairly new to Python/Django)
Reply all
Reply to author
Forward
0 new messages