Python 在 Django HttpResponse 对象中为 Shopify 应用设置 Content-Type
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17548414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Content-Type in Django HttpResponse object for Shopify App
提问by winter
I'm working on a Shopify app using Django, which I am hosting on a VPS with nginx and gunicorn.
我正在使用 Django 开发 Shopify 应用程序,我正在使用 nginx 和 gunicorn 托管在 VPS 上。
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.
我正在尝试将 HttpResponse 对象的 Content-Type 更改为application/liquid
,以便我可以使用 Shopify 的应用程序代理功能,但它似乎不起作用。
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:
根据Django docs,我应该设置response[''content_type]
以便Content-Type
在标题中设置。不幸的是,当我转到 views.py 中与此函数对应的 URL 时,我得到 200 响应,但 Content-Type 没有改变,Content-Length 为 0。这是我的响应标头:
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:
如果我更改response['content_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"
Any ideas on how I can change the Content-Type of the response? Might this be a problem with my nginx or gunicorn configurations?
关于如何更改响应的 Content-Type 的任何想法?这可能是我的 nginx 或 gunicorn 配置的问题吗?
Thanks for your help!
谢谢你的帮助!
回答by Matt
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:
一个快速提示,您可以将其添加到NGINX 配置的http
或server
块部分,这样您就不必在视图和其他 Django 代码中指定编码:
charset utf-8;
charset_types text/css application/json text/plain application/liquid;
回答by Paulo Bu
Following the instructions from the docsit 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!
希望这可以帮助!
回答by winter
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!
谢谢大家的帮助!
回答by LostMyGlasses
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:
只是为了扩展其他答案,如果HttpResponse
对象已经存在并且需要在实例化后设置其 MIME 类型(例如,在调用父方法时),可以通过以下方式实现:
response = super(...) # This returns some HttpResponse object
response["Content-Type"] = "application/liquid"
return response