Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango custom templatetag not getting request in context
    text
    copied!<p>I am using django 1.4 and trying to convert the code described at the end of <a href="http://dashdrum.com/blog/2010/03/full-url-in-django/" rel="nofollow">this article</a> into a customtag. This means I need access to the is_secure and site_name values from the request. Here is my CONTEXT_PROCESSORS in settings.py:</p> <pre><code>CONTEXT_PROCESSORS = ( 'django.core.context_processors.request', 'django.contrib.auth.context_processors.auth', ) </code></pre> <p>Here is my template tag code:</p> <pre><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 </code></pre> <p>In my view code I am using the new render shortcut like so:</p> <pre><code>return render(request, 'myapp/mytemplate.html', {'foo':bar}) </code></pre> <p>And I am calling it like this in the template:</p> <pre><code>{% full_static_url "images/logo.gif" %} </code></pre> <p>The problem is, when it gets to the line <strong>request = context['request']</strong> it throws a KeyError because 'request' is not in context.</p> <p>What am I doing wrong here?</p> <p>Full traceback is:</p> <pre><code>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' </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload