Django的JsonResponse - 显示错误的值 [英] JsonResponse from Django - Incorrect values displayed

查看:403
本文介绍了Django的JsonResponse - 显示错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:当您使用chrome浏览器访问生成JSON作为输出的页面时。

Update: When you access a page that produces JSON as output with the chrome browser. Incorrect results are shown.

考虑这个生成json的django代码。当您 json.dumps a long 时,两位数字与预期值不同。这是代码:

Consider this django code that produces json. When you json.dumps a long, the two digits are different from the expected value. Here is the code:

from django.http import JsonResponse, HttpResponse
import json

def dumps1(request):
    return JsonResponse({'pk': 456011173705795438 })


def dumps2(request):
    return HttpResponse(json.dumps({'pk': 455928532169112023 }))

自然,你会期望 dumps1 返回,HttpResponse与正文为 {'pk':456011173705795438} 而dumps2应该返回 {'pk' :455928532169112023} 但实际结果是

Naturally, you would expect dumps1 to return and HttpResponse with the body as {'pk': 456011173705795438 } while dumps2 should return {'pk': 455928532169112023 } but actual results are

{
    pk: 456011173705795460
}

{
    pk: 455928532169112000
}

请注意,两种情况下,数字中的最后两位都有变化。如果打开django shell并输入json.dumps({'pk':455928532169112023}),则会产生正确的输出。

Notice that in both cases, the last two digits in the number have changed. If you open the django shell and type in json.dumps({'pk': 455928532169112023 }) the correct output is produced.

Django 1.8.5和python 2.7。 6(ipython 4.0.b1)

Django 1.8.5 and python 2.7.6 ( ipython 4.0.b1)

这是一个错误还是功能?

Is this a bug or a feature?

推荐答案

之后@ user2719875的 answer 。我寻找可能的其他问题。我以前使用过铬。检查firefox然后wget,发现结果是正确的。然后我禁用了chrome中的一些扩展。问题出在Chrome JsonView扩展。 (我不会链接到它,因为它是错误的)。扩展名正确显示值!这是一个浪费了我几个小时的时间的错误。

After @user2719875 's answer. I looked for possible other problems. I was using chrome before. Checked in firefox and then wget and found that the result was correct. Then I disabled some extensions in chrome. The problem was in Chrome JsonView extension. (I will not link to it because it's buggy). The extension was displaying the values incorrectly!! That was a bug that wasted many hours of my time.

更新:之后,我尝试了Chrome商店中的几个json查看器。似乎产生正确结果的唯一一个是Tulios的JSON查看器

Update: After that I tried out several json viewers in the chrome store. The only one that seemed to produce the right result was 'JSON Viewer' by 'Tulios'

这篇关于Django的JsonResponse - 显示错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆