Friday, December 26, 2014

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

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

On Tuesday, December 23, 2014 6:04:35 AM UTC-6, Alexandre Provencio wrote:
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'


On Tue, Dec 23, 2014 at 12:03 AM, Collin Anderson <cmawe...@gmail.com> wrote:
> 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
>
>
> On Saturday, December 20, 2014 3:27:00 PM UTC-6, Alexandre Provencio wrote:
>>
>> 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!
>
> --
> 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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/6645593a-5e3c-45d1-808c-c5a03d5643e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment