首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Shopify App的Django HttpResponse对象中设置内容类型

在Shopify App的Django HttpResponse对象中设置内容类型
EN

Stack Overflow用户
提问于 2013-07-09 20:41:22
回答 4查看 47.4K关注 0票数 22

我正在开发一个使用Django的Shopify应用程序,我将它托管在一个带有nginx和gunicorn的VPS上。

我试图将HttpResponse对象的Content-Type更改为application/liquid,这样我就可以使用Shopify的application proxy feature,但它似乎不起作用。

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

代码语言:javascript
复制
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

根据Django docs,我应该设置response[''content_type]以便在header中设置Content-Type。不幸的是,当我转到views.py中与此函数对应的URL时,我得到了一个200响应,但Content-Type没有改变,Content-Length为0。下面是我的响应头部:

代码语言:javascript
复制
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"

如果将response['content_type']更改为response['Content-Type'],则会得到以下标头:

代码语言:javascript
复制
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"

关于如何更改响应的Content-Type,有什么建议吗?这可能是我的nginx或gunicorn配置的问题吗?

谢谢你的帮忙!

EN

回答 4

Stack Overflow用户

发布于 2013-07-09 20:48:20

尝试以下操作:

代码语言:javascript
复制
def featured(request):
    content = '<html>test123</html>'

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

    return response

快速提示,您可以将此代码添加到NGINX配置的httpserver块中,这样您就不必在视图和其他Django代码中指定编码:

代码语言:javascript
复制
charset utf-8;
charset_types text/css application/json text/plain application/liquid;
票数 18
EN

Stack Overflow用户

发布于 2013-07-09 21:57:31

所以这对我很有效:

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

谢谢大家的帮助!

票数 5
EN

Stack Overflow用户

发布于 2013-07-09 20:48:58

instructions from the docs之后,应该是这样的:

代码语言:javascript
复制
# set content_type
response = HttpResponse("",
                        content_type="application/liquid; charset=utf-8")
# add content
response.write('<html>test123</html>')

希望这能有所帮助!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17548414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档