Django custom templatetag not getting request in context

I am using django 1.4 and trying to convert the code described at the end of this article to customtag. This means that I need access to the is_secure and site_name values ​​from the request. Here are my CONTEXT_PROCESSORS in settings.py:

CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    'django.contrib.auth.context_processors.auth',
)

Here is my template tag code:

from django import template
register = template.Library()

@register.simple_tag(takes_context=True)
def full_static_url(context, url):
    request = context['request']
    scheme = 'http'
    if request.is_secure:
        scheme += 's'
    return scheme + '://' + request.site_name + context['STATIC_URL'] + url

In my view code, I use the new render shortcut like this:

return render(request, 'myapp/mytemplate.html', {'foo':bar})

And I call it like this in the template:

{% full_static_url "images/logo.gif" %}

The problem is that when it falls into the request = context ['request'] line , it throws a KeyError because the “request” is not in context.

What am I doing wrong here?

Full trace:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\Projects\blah\blah\myapp\views\myview.py" in manifestcosts
  44.     return render(request, 'myapp/mytemplate.html', {'foo':bar})
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in render
  44.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "C:\Python27\lib\site-packages\django\template\loader.py" in render_to_string
  176.         return t.render(context_instance)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  140.             return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
  134.         return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  823.                 bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
  74.             return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
  185.                         nodelist.append(node.render(context))
File "C:\Python27\lib\site-packages\django\template\base.py" in render
  1107.                     return func(*resolved_args, **resolved_kwargs)
File "C:\Projects\blah\blah\myapp\templatetags\mytags.py" in full_static_url
  25.     request = context['request']        #TODO this fails with an KeyError, don't know why
File "C:\Python27\lib\site-packages\django\template\context.py" in __getitem__
  54.         raise KeyError(key)

Exception Type: KeyError at /myapp/myurl/110505081136179000/
Exception Value: 'request'
+3
source share
4 answers

- TEMPLATE_CONTEXT_PROCESSORS += ("django.core.context_processors.request",) settings.py.

TEMPLATE_CONTEXT_PROCESSORS from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS .

+10

, , , - :

return render_to_response("myapp/template.html", {"some_var": a_value})

, RequestContext. , RequestContext render_to_response:

return render_to_response("myapp/template.html", {"some_var": a_value},
                          context_instance=RequestContext(request))

, render :

return render(request, "myapp/template.html", {"some_var": a_value})
+1

,

return render(request, 'myapp/mytemplate.html', {'foo':bar})

to

return render( RequestContext(request), 'myapp/mytemplate.html', {'foo':bar})

, - , 8 : p

0

template.Node render(). , "", .

- , RequestContext(request). , .

 def render(self, context):
      request = context['request']  # Failing here

 def render(self, context):
      request = RequestContext(context)['request']['request']

.

, . - ['request'] , , ,


EDIT: , , . :

request = context.get('request')
if request is None:
    return ''

, , .

0

All Articles