First time here? Check out the FAQ!

Revision history  [back]

What seems to fix the issue - add to settings.py:

import sys
reload(sys) #this brings method setdefaultencoding
sys.setdefaultencoding('utf-8')

As @Tvartom suggested - this issue is seen when the django memcache caching backend is used. I find it strange how django memcached cache backend works (even after the default encoding is set to utf-8):

>>> from django.core.cache import cache
>>> cache.set('key', u'\u1537')
>>> cache.get('key')
'\xe1\x94\xb7'

Note that the value comes back as encoded string, not as original value.

With Redis cache (and similarly with the LocMemCache) I get what I'd expect:

>>> cache.set('key', u'\u1537')
True
>>> cache.get('key')
u'\u1537'

Redis cache also works irrespective of the default encoding.

Because of that I would recommend to use django-redis-cache.

What seems to fix the issue - add to settings.py:

import sys
reload(sys) #this brings method setdefaultencoding
sys.setdefaultencoding('utf-8')

As @Tvartom suggested - this issue is seen when the django memcache caching backend is used. However I find it strange how django memcached cache backend works (even after the default encoding is set to utf-8):works:

>>> from django.core.cache import cache
>>> cache.set('key', u'\u1537')
>>> cache.get('key')
'\xe1\x94\xb7'

Note that the value comes back as encoded string, not as original value.

With Redis cache (and similarly with the LocMemCache) I get what I'd expect:

>>> cache.set('key', u'\u1537')
True
>>> cache.get('key')
u'\u1537'

Redis cache also works irrespective of the default encoding.

Because of that I would recommend to use django-redis-cache.