django: Fix for HttpResponse.set_cookie() TypeError: translate() takes exactly one argument (2 given)

What the hell is this!? It's the first strange quirk I've noticed in Django, that's what!

4pOg3
Time to freak out!

I'm still not sure exactly why this happens but it's the first time I've encountered it, even though I've been using the set_cookie() method for a while now.

So in testing, this was the sample code I had:

response = HttpResponse('')
response.set_cookie('cookie_name', 1)
return response

In theory, that would normally work. For some strange reason, it threw an exception at the 2nd line.

Apparently it doesn't like unicode for the cookie key. To fix it, wrap the key with str().

So now the code looks like this:

response.set_cookie(str('cookie_name'), 1)

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog