Setting Content-Type in Django HttpResponse object for Shopify App

流过昼夜 提交于 2019-12-04 23:59:52

Try the following:

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

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

    return response

A quick tip, you could add this into the http or server block part of your NGINX configuration so you don't have to specify the encoding within views and other Django code:

charset utf-8;
charset_types text/css application/json text/plain application/liquid;

Following the instructions from the docs it should be something like this:

# set content_type
response = HttpResponse("",
                        content_type="application/liquid; charset=utf-8")
# add content
response.write('<html>test123</html>')

Hope this helps!

So this worked for me:

def featured(request):
  response = HttpResponse("", content_type="application/liquid; charset=utf-8")
  response['Content-Length'] = len(content)
  response.write('<html>test123</html>')
  return response

Thanks, everyone, for the help!

Just to expand the other answers, if the HttpResponse object already exists and its MIME type needs to be set after instantiating it (for example, when invoking a parent method), it can be achieved this way:

response = super(...)  # This returns some HttpResponse object
response["Content-Type"] = "application/liquid"
return response
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!