5

I read this

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

But it seems there is no information how to activate this processor.

Here is my original question

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'

the code causing problem is

request = context['request'] in

from django import template

register = template.Library()


@register.inclusion_tag('userinfo.html',takes_context = True)
def userinfo(context):
 request = context['request']
 address = request.session['address']
 return {'address':address}
Chris Tang
  • 567
  • 7
  • 18
icn
  • 17,126
  • 39
  • 105
  • 141

2 Answers2

2

in settings.py

from django.conf import global_settings

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',
)
  • 1
    `Note that a settings file should not import from global_settings, because that’s redundant` --> https://docs.djangoproject.com/en/dev/topics/settings/#default-settings – user Apr 06 '14 at 18:35
  • That comment is about importing a variable simply to reuse it, which would happen anyway. Here, the code is adding to the default setting from global_settings and so needs to import it in order to have access to it. – M Somerville May 22 '14 at 10:19
2

I answered this here: How can I pass data to any template from any view in Django?

Also see the comments on my answer... you might want that bit of info too.

Community
  • 1
  • 1
Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
  • thanks , but i already passed "takes_context = True" and I have "django.core.context_processors.request" included in settings.py Why it still get KeyError when i call context['request'] – icn Feb 04 '10 at 19:07
  • Why don't you print context (or context.keys()) and see what's in there. Apparently, context doesn't have a "request" key, so see what keys it does have. – Andrew Johnson Feb 04 '10 at 19:51
  • sorry to ask again.. i got this Exception Value: Caught an exception while rendering: 'Context' object has no attribute 'keys' – icn Feb 04 '10 at 20:00
  • I'm sorry... I thought context was a dictionary, but it's a Context object. What does it output if you just print context? – Andrew Johnson Feb 04 '10 at 20:04
  • thanks a lot Andrew, It turns out i missed to pass "context_instance=RequestContext(request)" in the view code – icn Feb 04 '10 at 21:56