Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Shopify应用程序的Django HttpResponse对象中设置内容类型_Python_Django_Http_Httpresponse_Shopify - Fatal编程技术网

Python 在Shopify应用程序的Django HttpResponse对象中设置内容类型

Python 在Shopify应用程序的Django HttpResponse对象中设置内容类型,python,django,http,httpresponse,shopify,Python,Django,Http,Httpresponse,Shopify,我正在使用Django开发Shopify应用程序,我正在使用nginx和gunicorn在VPS上托管该应用程序 我正试图将HttpResponse对象的内容类型更改为application/liquid,这样我就可以使用Shopify,但它似乎不起作用 以下是我认为与我的代码相关的部分: from django.shortcuts import render_to_response, render from django.http import HttpResponse from django

我正在使用Django开发Shopify应用程序,我正在使用nginx和gunicorn在VPS上托管该应用程序

我正试图将HttpResponse对象的内容类型更改为
application/liquid
,这样我就可以使用Shopify,但它似乎不起作用

以下是我认为与我的代码相关的部分:

from django.shortcuts import render_to_response, render
from django.http import HttpResponse
from django.template import RequestContext
import shopify
from shopify_app.decorators import shop_login_required

def featured(request):
   response = HttpResponse()
   response['content_type'] = 'application/liquid; charset=utf-8'
   response['content'] = '<html>test123</html>'
   response['Content-Length'] = len(response.content)
   return response
如果我将
response['content\u type']
更改为
response['content-type']
,我会得到以下标题:

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 09 Jul 2013 12:34:09 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 3097
Connection: keep-alive
X-Request-Id: 76e67e04b753294a3c37c5c160b42bcb
vary: Accept-Encoding
status: 200 OK
x-shopid: 2217942
x-request-id: 6e63ef3a27091c73a9e3fdaa03cc28cb
x-ua-compatible: IE=Edge,chrome=1
p3p: CP="NOI DSP COR NID ADMa OPTa OUR NOR"
content-encoding: gzip
P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR"
关于如何更改响应的内容类型,有什么想法吗?这可能是我的nginx或gunicorn配置的问题吗

谢谢你的帮助

尝试以下操作:

def featured(request):
    content = '<html>test123</html>'

    response = HttpResponse(content, content_type='application/liquid')
    response['Content-Length'] = len(content)

    return response
下面的例子应该是这样的:

# set content_type
response = HttpResponse("",
                        content_type="application/liquid; charset=utf-8")
# add content
response.write('<html>test123</html>')
#设置内容#u类型
响应=HttpResponse(“”,
内容物类型=“应用/液体;字符集=utf-8”)
#添加内容
response.write('test123')
希望这有帮助

这对我来说很有效:

def featured(request):
  response = HttpResponse("", content_type="application/liquid; charset=utf-8")
  response['Content-Length'] = len(content)
  response.write('<html>test123</html>')
  return response
def精选(请求):
响应=HttpResponse(“,content_type=“应用程序/液体;字符集=utf-8”)
响应['Content-Length']=len(内容)
response.write('test123')
返回响应

谢谢大家的帮助

仅扩展其他答案,如果
HttpResponse
对象已经存在,并且在实例化后需要设置其MIME类型(例如,调用父方法时),则可以通过以下方式实现:

response = super(...)  # This returns some HttpResponse object
response["Content-Type"] = "application/liquid"
return response

Matt,感谢您提供有关nginx配置的提示!我一定会用的。不幸的是,我尝试了您的代码建议,它返回了一个411HTTP错误(需要长度)。试图将
length
Content length
的值指定为HttpResponse中的参数会导致Django错误(意外的关键字参数和关键字不能是表达式)。
len(Content)
应为
len(Content.encode('utf-8)
Paulo,谢谢你的帮助!事实证明,当我尝试这项技术时,我收到了一个411长度必需的错误。但是!将此与响应['Content-Length]=len(Content)相结合工作正常。这很奇怪,因为我实际上没有指定内容…除非response.write自动将
test123
添加到响应的内容中?无论如何,感谢您的帮助!确实如此。文档说,但是如果您想增量添加内容,可以将响应用作类似文件的对象。因此
.write
添加内容nt到响应:)您不必为内容指定任何内容,即response=HttpResponse(content_type='image/png'),然后像以前那样对其进行写入。值得注意的是,您可以使用与length、response['content-type'相同的方式获取/设置内容类型
def featured(request):
  response = HttpResponse("", content_type="application/liquid; charset=utf-8")
  response['Content-Length'] = len(content)
  response.write('<html>test123</html>')
  return response
response = super(...)  # This returns some HttpResponse object
response["Content-Type"] = "application/liquid"
return response