Setting Content-Type in Django HttpResponse object

2019-04-18 03:44发布

问题:

I'm working on a Shopify app using Django, which I am hosting on a VPS with nginx and gunicorn.

I am trying to change the Content-Type of an HttpResponse object to application/liquid, so that I can use Shopify's application proxy feature, but it doesn't appear to be working.

Here is what I believe to be the relevant section of my code:

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

According to the Django docs, I should set response[''content_type] in order to set Content-Type in the header. Unfortunately, when I go to the URL corresponding to this function in views.py, I get a 200 response but the Content-Type has not changed and Content-Length is 0. Here are my response headers:

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 09 Jul 2013 12:26:59 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
X-Request-Id: 2170c81fb16d18fc9dc056780c6d92fd
content: <html>test123</html>
vary: Cookie
content_type: application/liquid; charset=utf-8
P3P: CP="NOI DSP COR NID ADMa OPTa OUR NOR"

If I change response['content_type'] to response['Content-Type'], I get the following headers:

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"

Any ideas on how I can change the Content-Type of the response? Might this be a problem with my nginx or gunicorn configurations?

Thanks for your help!

回答1:

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;


回答2:

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!



回答3:

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!



回答4:

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