HttpResponse.delete_cookie() gives me an error

1,115 views
Skip to first unread message

larsendt

unread,
Aug 18, 2010, 5:58:27 PM8/18/10
to Django users
The relevant code is simple:

response = HttpResponseRedirect('/colors/')
response.delete_cookie(key=color_id, path='/')
return response

But when I try to run it, I get the error:

TypeError at /colors/delete/colorcompare29108/
translate() takes exactly one argument (2 given)
Exception Location: /usr/lib/python2.6/string.py in translate,
line 493

The value of color_id is just a string (in this case it was
colorcompare29108).

Any suggestions?

Rolando Espinoza La Fuente

unread,
Aug 19, 2010, 12:12:47 AM8/19/10
to django...@googlegroups.com

Could you provide full traceback?

Rolando Espinoza La fuente
www.insophia.com

Dane Larsen

unread,
Aug 19, 2010, 12:42:31 AM8/19/10
to django...@googlegroups.com
Environment:

Request Method: GET
Request URL: http://*********.com/colors/delete/colorcompare89441/
Django Version: 1.2.1
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/home/dane/larsendt/../larsendt/colorcompare/views.py" in delete
  86. response.delete_cookie(key=color_id, path='/')
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in delete_cookie
  390.                         expires='Thu, 01-Jan-1970 00:00:00 GMT')
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in set_cookie
  376.         self.cookies[key] = value
File "/usr/lib/python2.6/Cookie.py" in __setitem__
  585.         self.__set(key, rval, cval)
File "/usr/lib/python2.6/Cookie.py" in __set
  578.         M.set(key, real_value, coded_value)
File "/usr/lib/python2.6/Cookie.py" in set
  454.         if "" != translate(key, idmap, LegalChars):
File "/usr/lib/python2.6/string.py" in translate
  493.         return s.translate(table, deletions)

Exception Type: TypeError at /colors/delete/colorcompare89441/
Exception Value: translate() takes exactly one argument (2 given)

Dane Larsen

unread,
Aug 19, 2010, 12:14:59 PM8/19/10
to django...@googlegroups.com
Ahh. Problem solved. 

I was calling: 
    response.delete_cookie(key=color_id)

color_id was a string.

When I did this:

for key in request.COOKIES.keys():
    if key == color_id:
        response.delete_cookie(key=key)

It worked.

It seems that passing a unicode string rather than a python string is what causes the problem.
The only difference between key and color_id is this:

color_id u'colorcompare73936'
key 'colorcompare73936'

color_id has the u'...'

I then tried:

    response.delete_cookie(key=str(color_id))

and it worked fine.

Should I report this as a bug?

Rolando Espinoza La Fuente

unread,
Aug 19, 2010, 1:02:08 PM8/19/10
to django...@googlegroups.com
On Thu, Aug 19, 2010 at 12:14 PM, Dane Larsen <dane.t...@gmail.com> wrote:
[...]

> I then tried:
>     response.delete_cookie(key=str(color_id))
> and it worked fine.
> Should I report this as a bug?

Cookie.Morsel does not handle unicode strings, mainly because
string.translate expects
two arguments where unicode.translate expects only one.

A fix to django.http.CompatCookie could be:

+ def __setitem__(self, K, V):
+ # Cookie.Morsel does not handle unicode keys
+ return SimpleCookie.__setitem__(self, smart_str(K), V)

But I'm not sure if you should try to use an unicode key in a cookie.

Dane Larsen

unread,
Aug 19, 2010, 1:24:57 PM8/19/10
to django...@googlegroups.com
Well, regardless, it works if you convert it to a str first.

Thanks for the help!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages