Reputation:
I have a bunch of values which I would like to send from views.py
function to my template in Django. I saw some topics that the best way is by json
format. So I did so. But because my values are not ascii
I'm using a upgraded version which worked in normal Http response
but don't work in JSON response
.
Here is my code
base = {weather_main_key : weather_main_values, wind_speed_key : wind_speed_value + "m", wind_deg_key : wind_deg_value, base_temp_key : base_temp_value + " ℃", base_press_key : base_press_value + " mbar", base_hum_key : base_hum_value + " % " }
base = json.dumps(base, ensure_ascii=False).encode('utf8')
return JsonResponse(json.dumps(base))
So I had an error msg
In order to allow non-dict objects to be serialized set the safe parameter to False.
So I did as it told me
JsonResponse(json.dumps(base, safe=False, ensure_ascii=False).encode('utf8'))
And now the error is
__init__() got an unexpected keyword argument 'safe'
And I can't move...
Upvotes: 0
Views: 717
Reputation: 599610
Whoah, triple encoding. Why are you doing that?
You serialize to json. Then, inside the call to JsonResponse, you serialize to json again. But JsonResponse itself does serialization, so you've serialized three times.
Stop that; just pass the dict to JsonResponse.
base = {weather_main_key : weather_main_values, wind_speed_key : wind_speed_value + "m", wind_deg_key : wind_deg_value, base_temp_key : base_temp_value + " ℃", base_press_key : base_press_value + " mbar", base_hum_key : base_hum_value + " % " }
return JsonResponse(base)
Upvotes: 3