【发布时间】:2021-02-12 23:54:52
【问题描述】:
我是 Django REST 框架的新手,我尝试创建一个视图
class TestView(APIView):
def get(self, request, *args, **kwargs):
return Response("Hello world")
当我在 python 中使用 curl 或请求模块发送请求时:
curl -X GET localhost:8000/test/
我得到“Hello world”作为响应,我真正需要的是没有额外双引号的 Hello world 字符串。 我尝试了不同的 content_types,但到目前为止它们都没有奏效。 我怎样才能摆脱双引号?
【问题讨论】:
-
返回 Django
HttpResponse而不是 rest_frameworkResponse。 rest_frameworkResponse会将字符串转换为 JSON 并添加引号 -
@IainShelvington 有什么办法可以改变这种行为吗?
-
@Maryam 您可能应该使用自定义渲染器。看看django-rest-framework.org/api-guide/renderers
标签: python django api django-rest-framework