I'm trying to prevent a string (in this case the value
variable) in a POST request being escaped as it's to be stored in JSON. My code is
def addProduct(request):
if request.POST:
post = {}
for key in request.POST:
value = request.POST[key].encode('utf-8')
try:
value = json.loads(value).encode('utf-8')
except Exception:
pass
post[key] = value.encode('utf-8')
doc = json.dumps(post)
Debugging I can see value
is of type unicode which I believe is how Django handles request objects. The actual string although unicode doesn't get its special characters escaped until post[key] = value
. If I try to change this to post[key] = value.encode('utf-8')
to prevent it getting escaped I get the error: 'ascii' codec can't decode byte 0xe2 in position 38: ordinal not in range(128)
Any ideas?