简体   繁体   English

JsonResponse未使用Django将内容类型设置为application / json

[英]JsonResponse not setting content-type to application/json using django

Here is the response returned by my view in django. 这是我在django中返回的响应。 For some reason the web inspector recognizes that the content_type is application/json but when using httpie, it recognizes it as text/html. 出于某种原因,Web检查器会识别到content_type是application / json,但是在使用httpie时,它会将其识别为text / html。 Am I doing something wrong, which do i trust? 我做错了什么,我可以信任哪个?

Here is my view code: 这是我的查看代码:

def RegistrationView(request):
if request.method == 'GET':
    reg_user = User.objects.create(username=str(User.objects.all().count()+1), password=str(uuid.uuid4()))
    reg_user.save()
    serialized_user = UserSerializer(reg_user)
    json_rend = JSONRenderer()
    import ast
    return JsonResponse(ast.literal_eval(json_rend.render(serialized_user.data)))
return HttpResponse("woah")

Here is the comparison between httpie and the safari web inspector: 这是httpie和safari网络检查器之间的比较: 在此处输入图片说明

在此处输入图片说明

In the first screenshot, you are requesting localhost:8000/lkd/. 在第一个屏幕截图中,您正在请求localhost:8000 / lkd /。 In the second, you are requesting localhost:8000/lkd - note, no trailing slash. 在第二个中,您请求的是localhost:8000 / lkd-注意,不带斜杠。 In that second case, Django is sending a 301 response which redirects you to the address with the slash, as the rest of that screenshot shows. 在第二种情况下,Django发送301响应,将您重定向到带有斜线的地址,如屏幕快照的其余部分所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM