how to activate DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST

573 views
Skip to first unread message

weiwei

unread,
Feb 4, 2010, 1:41:47 PM2/4/10
to Django users
"DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST
If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every
RequestContext will contain a variable request, which is the current
HttpRequest. Note that this processor is not enabled by default;
you'll have to activate it. " from this page

http://docs.djangoproject.com/en/dev/ref/templates/api/

But i didn't find how to activate it

Here is my question

http://stackoverflow.com/questions/2160261/access-request-in-django-custom-template-tags

after i followed the answer i still got errors

TemplateSyntaxError at / Caught an exception while rendering:
'request' Original Traceback (most recent call last): File "C:
\Python25\lib\site-packages\django\template\debug.py", line 71, in
render_node result = node.render(context) File "C:\Python25\lib\site-
packages\django\template__init__.py", line 936, in render dict =
func(*args) File "c:\...\myapp_extras.py", line 7, in login request =
context['request'] File "C:\Python25\lib\site-packages\django\template
\context.py", line 44, in getitem raise KeyError(key) KeyError:
'request'

tho code causing problem is

request = context['request']


Thanks for any help!

Karen Tracey

unread,
Feb 4, 2010, 2:17:39 PM2/4/10
to django...@googlegroups.com
On Thu, Feb 4, 2010 at 1:41 PM, weiwei <online.service.com@gmail.com> wrote:
"DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST
If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every
RequestContext will contain a variable request, which is the current
HttpRequest. Note that this processor is not enabled by default;
you'll have to activate it. " from this page

http://docs.djangoproject.com/en/dev/ref/templates/api/

But i didn't find how to activate it


You activate it by including it in TEMPLATE_CONTEXT_PROCESSORS in settings.py.  That is all you have to do.

 
Here is my question

http://stackoverflow.com/questions/2160261/access-request-in-django-custom-template-tags

after i followed the answer i still got errors


I do not see where you show the view code in that question. Possibly you are not using a RequestContext to render the template?

Karen

weiwei

unread,
Feb 4, 2010, 2:39:03 PM2/4/10
to Django users
Thanks..

Here is my code

from django import template
from django.template import RequestContext

register = template.Library()


@register.inclusion_tag('userinfo.html',takes_context = True)
def userinfo(context):
request = context['request']
address = request.session['address']
return {'address':address}

and request = context['request'] causing problem, seems context
doesn't have a key 'request'


And i do have

TEMPLATE_CONTEXT_PROCESSORS =(
"django.core.context_processors.request",
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
)

in settings.py
On Feb 4, 11:17 am, Karen Tracey <kmtra...@gmail.com> wrote:


> On Thu, Feb 4, 2010 at 1:41 PM, weiwei <online.service....@gmail.com> wrote:
> > "DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST
> > If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every
> > RequestContext will contain a variable request, which is the current
> > HttpRequest. Note that this processor is not enabled by default;
> > you'll have to activate it. " from this page
>
> >http://docs.djangoproject.com/en/dev/ref/templates/api/
>
> > But i didn't find how to activate it
>
> You activate it by including it in TEMPLATE_CONTEXT_PROCESSORS in
> settings.py.  That is all you have to do.
>
> > Here is my question
>

> >http://stackoverflow.com/questions/2160261/access-request-in-django-c...

Karen Tracey

unread,
Feb 4, 2010, 2:54:26 PM2/4/10
to django...@googlegroups.com
On Thu, Feb 4, 2010 at 2:39 PM, weiwei <online.service.com@gmail.com> wrote:
Thanks..

Here is my code


You repeated the code for the template tag and the context processors setting; that's not what I asked for.

I still don't see the code for the view that renders the template that includes the template tag you are working with.  That is the code that must specify a RequestContext if you want to access the variables set by the request context processor in your template tag. The context processor won't set variables in a plain Context, the view code (or whatever code is supplying the context for the template render) must specify a RequestContext. See for example 'some_view' under:

http://docs.djangoproject.com/en/dev/ref/templates/api/#id1

Karen

weiwei

unread,
Feb 4, 2010, 3:15:42 PM2/4/10
to Django users, kmtr...@gmail.com
Thanks a lot, i did miss to pass

context_instance=RequestContext(request) in

my view code

def access(request):
return
render_to_response('tool.html',context_instance=RequestContext(request))

after I added "context_instance=RequestContext(request)"

It is working now like a charm!

Thanks again!


On Feb 4, 11:54 am, Karen Tracey <kmtra...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages