TyMaszWeb / django-cookie-law

Helps your Django project comply with the EU cookie regulations by displaying a cookie information banner until it is dismissed by the user
BSD 2-Clause "Simplified" License
140 stars 69 forks source link

KeyError 'request' #9

Closed qcaron closed 9 years ago

qcaron commented 9 years ago

Hey guys,

When in the admin, I keep getting KeyError 'request' errors:

File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/cookielaw/templatetags/cookielaw_tags.py", line 18, in render_tag
   if context['request'].COOKIES.get('cookielaw_accepted', False)

Here is my whole traceback:

Internal Server Error: /favicon.ico
Traceback (most recent call last):
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 150, in get_response
   response = callback(request, **param_dict)
 File "./pickmystartup/views.py", line 1324, in page_not_found
   return HttpResponseNotFound(render_to_string('404.html'))
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/loader.py", line 172, in render_to_string
   return t.render(Context(dictionary))
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/base.py", line 148, in render
   return self._render(context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/test/utils.py", line 88, in instrumented_test_render
   return self.nodelist.render(context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
   bit = self.render_node(node, context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
   return node.render(context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 126, in render
   return compiled_parent._render(context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/test/utils.py", line 88, in instrumented_test_render
   return self.nodelist.render(context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
   bit = self.render_node(node, context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
   return node.render(context)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/classytags/core.py", line 106, in render
   return self.render_tag(context, **kwargs)
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/cookielaw/templatetags/cookielaw_tags.py", line 18, in render_tag
   if context['request'].COOKIES.get('cookielaw_accepted', False):
 File "/srv/data/web/vhosts/default/local/local/lib/python2.7/site-packages/django/template/context.py", line 70, in __getitem__
   raise KeyError(key)
KeyError: 'request'

Is this related to the use of django-cookie-law or not? Any solution or thought?

paolodina commented 9 years ago

Hi, probably django.template.context_processors.request is not in TEMPLATE_CONTEXT_PROCESSORS.

qcaron commented 9 years ago

Indeed! I will give it a try, thanks :)

qcaron commented 9 years ago
ImportError at /
No module named context_processors

I am using django 1.7.7, seems django.template.context_processors.request does not exist yet in this version.

qcaron commented 9 years ago

And I am using 'django.core.context_processors.request'.

qcaron commented 9 years ago

Ok I have a little bit more information on this error: for example, it is raised when using AJAX with JsonResponse in views. How may I avoid this error ??? This is important guys I really need help on this one =/

qcaron commented 9 years ago

Should we test for key request first ?

if context.get('request') and context['request'].COOKIES.get('cookielaw_accepted', False):
     return ''
piotrkilczuk commented 9 years ago

Hi @qcaron,

Unfortunately django-cookie-law is not being tested using Django 1.7 at the moment, so you'd have to work out way to handle this: https://github.com/TyMaszWeb/django-cookie-law/blob/master/.travis.yml#L7

From your traceback it seems like you're using the {% cookielaw_banner %} tag in your 404.html template (or the one 404.html is extending).

There are a few issues here:

It'd be great if you could take care of the last point and open a PR. Otherwise I'll try to look into this in the upcoming week.

piotrkilczuk commented 9 years ago

Hi @qcaron

Can you please confirm that with version 1.0.6 this is fixed?

Thanks

paolodina commented 9 years ago

Shouldn't this https://github.com/TyMaszWeb/django-cookie-law/blob/master/cookielaw/templatetags/cookielaw_tags.py#L25 use elif instead of if?

qcaron commented 9 years ago

@centralniak I should be able to do some tests tomorrow ;) But @paolodina is right, an error may be raised without using an elif here.

qcaron commented 9 years ago

@centralniak I confirm you that I still get the error.

qcaron commented 9 years ago

The elif is missing. Except that it should be good :)