[Django]-Setting Content-Type in Django HttpResponse object for Shopify App

22πŸ‘

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;
πŸ‘€Matt

5πŸ‘

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!

πŸ‘€winter

4πŸ‘

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!

πŸ‘€Paulo Bu

3πŸ‘

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
πŸ‘€LostMyGlasses

Leave a comment