Django JsonResponse返回contenttype text/html而不是application/json

2024-09-30 12:13:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做的一个django web项目遇到了一个死胡同,我似乎找不到任何答案。 我试图测试一个简单的视图:

def list(request):
    return JsonResponse( {"foo": "bar"} )

似乎一切顺利。如果我在我的浏览器上打开这个站点并检查页面信息,它会显示“Type:application/json”。在

但是,当我在travis ci上运行以下测试时:

^{pr2}$

我有以下失败:

FAIL: test_content_type (researchlibrary.api.tests.test_list.ListTests)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "/home/travis/build/FUB-HCC/ACE-Research-Library/researchlibrary/api/tests/test_list.py", line 25, in test_content_type
    self.assertEqual(response['content-type'], 'application/json')
AssertionError: 'text/html' != 'application/json'
- text/html
+ application/json

网址都很好。测试收到正确的页面,只是类型显示为text/html而不是application/json,我不知道为什么会这样。在

有人知道为什么会这样吗?在

编辑:更改self.client.get('/api/list')到self.client.get('/api/list/')解决了问题。在


Tags: texttestselfclienttravisapijsonapplication
1条回答
网友
1楼 · 发布于 2024-09-30 12:13:41

看来

self.client.get('/api/list')

导致错误页面(因此为text/html内容类型)。在

编辑:根据LudwikTrammer的说法,不是错误页面,而是http重定向。在

改变

^{pr2}$

self.client.get('/api/list/') 

解决了这个问题。在

相关问题 更多 >

    热门问题