(yet another) Custom templatetag raising KeyError when DEBUG=False

71 views
Skip to first unread message

Alexandre Provencio

unread,
Dec 20, 2014, 4:27:00 PM12/20/14
to django...@googlegroups.com
Hello all, this is a cross post i made on stackoverflow
http://stackoverflow.com/questions/27503581/yet-another-custom-templatetag-raising-keyerror-when-debug-false

I'm trying to use a custom templatetag (this one actually:
https://djangosnippets.org/snippets/2875/), on my project which works
fine as long as DEBUG=True. When it's False, the relevant error part
is:

File "...app/templatetags/helper_tags.py", line 15, in change_lang
path = context['request'].path
File "...local/lib/python2.7/site-packages/django/template/context.py",
line 56, in __getitem__
raise KeyError(key)
KeyError: 'request'

I've seen a lot of questions like this and I have already done the
settings that all of them seen to suggest which are:

1) views.py uses django.shortcuts.render:

from django.shortcuts import render
def home(request):
return render(request, 'home.html')

2) settings.py contains:

ALLOWED_HOSTS = ['*']

from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
"django.core.context_processors.request",
)

I've also created a new project with the same Django version (1.6.2)
that tries to imitate the problematic project as much as possible, and
for my despair it works fine also when DEBUG=False.

Any hints on this is very much appreciated.

Cheers!

Collin Anderson

unread,
Dec 22, 2014, 9:03:47 PM12/22/14
to django...@googlegroups.com
Hi,

Do you have more of your traceback?

Are you sure this is your "home" view that's being called?

Is this happening in an included template?

Collin

Alexandre Provencio

unread,
Dec 23, 2014, 7:04:35 AM12/23/14
to django...@googlegroups.com
Hi Collin, thanks for answering.

Yes this is the home view, but just to clarify, almost all of the
views of the project follow this pattern of a function that returns
the render shortcut. The templates of the views also follow the a
pattern in the sense they all extend from base.html, which is where
the calls for the templatetag are done. This is the full traceback:

0 errors found
December 23, 2014 - 09:53:07
Django version 1.6.2, using settings 'foo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py",
line 206, in __call__
response = self.get_response(request)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/base.py",
line 155, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/base.py",
line 238, in handle_uncaught_exception
return callback(request, **param_dict)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/utils/decorators.py",
line 99, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/views/defaults.py",
line 46, in server_error
return http.HttpResponseServerError(template.render(Context({})))
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 140, in render
return self._render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 134, in _render
return self.nodelist.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 840, in render
bit = self.render_node(node, context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/debug.py",
line 78, in render_node
return node.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/loader_tags.py",
line 123, in render
return compiled_parent._render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 134, in _render
return self.nodelist.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 840, in render
bit = self.render_node(node, context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/debug.py",
line 78, in render_node
return node.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/defaulttags.py",
line 196, in render
nodelist.append(node.render(context))
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 1125, in render
return func(*resolved_args, **resolved_kwargs)
File "/home/xand/Documentos/src/Projetos/django-foo/app/templatetags/helper_tags.py",
line 14, in change_lang
path = context['request'].path
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/context.py",
line 56, in __getitem__
raise KeyError(key)
KeyError: 'request'
[23/Dec/2014 09:53:18] "GET /en/home HTTP/1.1" 500 59
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py",
line 206, in __call__
response = self.get_response(request)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/base.py",
line 155, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/base.py",
line 238, in handle_uncaught_exception
return callback(request, **param_dict)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/utils/decorators.py",
line 99, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/views/defaults.py",
line 46, in server_error
return http.HttpResponseServerError(template.render(Context({})))
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 140, in render
return self._render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 134, in _render
return self.nodelist.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 840, in render
bit = self.render_node(node, context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/debug.py",
line 78, in render_node
return node.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/loader_tags.py",
line 123, in render
return compiled_parent._render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 134, in _render
return self.nodelist.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 840, in render
bit = self.render_node(node, context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/debug.py",
line 78, in render_node
return node.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/defaulttags.py",
line 196, in render
nodelist.append(node.render(context))
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 1125, in render
return func(*resolved_args, **resolved_kwargs)
File "/home/xand/Documentos/src/Projetos/django-foo/app/templatetags/helper_tags.py",
line 14, in change_lang
path = context['request'].path
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/context.py",
line 56, in __getitem__
raise KeyError(key)
KeyError: 'request'
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py",
line 206, in __call__
response = self.get_response(request)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/base.py",
line 155, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/core/handlers/base.py",
line 238, in handle_uncaught_exception
return callback(request, **param_dict)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/utils/decorators.py",
line 99, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/views/defaults.py",
line 46, in server_error
return http.HttpResponseServerError(template.render(Context({})))
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 140, in render
return self._render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 134, in _render
return self.nodelist.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 840, in render
bit = self.render_node(node, context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/debug.py",
line 78, in render_node
return node.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/loader_tags.py",
line 123, in render
return compiled_parent._render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 134, in _render
return self.nodelist.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 840, in render
bit = self.render_node(node, context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/debug.py",
line 78, in render_node
return node.render(context)
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/defaulttags.py",
line 196, in render
nodelist.append(node.render(context))
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/base.py",
line 1125, in render
return func(*resolved_args, **resolved_kwargs)
File "/home/xand/Documentos/src/Projetos/django-foo/app/templatetags/helper_tags.py",
line 14, in change_lang
path = context['request'].path
File "/home/xand/.virtualenvs/django-foo/local/lib/python2.7/site-packages/django/template/context.py",
line 56, in __getitem__
raise KeyError(key)
KeyError: 'request'


> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e1236721-cc22-4554-82b6-ddb2a9d068e3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Collin Anderson

unread,
Dec 26, 2014, 4:23:19 PM12/26/14
to django...@googlegroups.com
Hi,

I see what's going on. This is your 500.html server error template that's being rendered, and unfortunately, you don't get context processors in that template. Sorry. The idea is to handle cases where even one of your middleware or context processors is failing.

See the line:
    return http.HttpResponseServerError(template.render(Context({})))

I think your best option is just to handle not having the request object available.

To be clear, there must be some _other_ error that's happening that's causing the 500 in the first place. If you configure ADMINS, you'll get an email with the traceback.

Collin

Alexandre Provencio

unread,
Dec 27, 2014, 5:47:25 PM12/27/14
to django...@googlegroups.com
Collin, I got the bug!

I was in the wrong path, it had nothing to do with request or all the
other things I mentioned; translation config problems were the
culprit. Your explanation was very helpful to put me on the right
trace!

Thank you so much!!!

- Alexandre
> https://groups.google.com/d/msgid/django-users/6645593a-5e3c-45d1-808c-c5a03d5643e1%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages