2013-07-09 28 views
17

我正在使用Django的Shopify應用程序,我使用nginx和gunicorn在VPS上託管該應用程序。在Shopify App中設置Django HttpResponse對象的內容類型

我試圖將HttpResponse對象的內容類型更改爲application/liquid,以便我可以使用Shopify的application proxy feature,但它似乎不工作。

以下是我認爲是我的代碼的相關章節:

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,我應該爲了設置Content-Type在頭設置response[''content_type]。不幸的是,當我去到對應於views.py此功能的URL,我得到一個200響應,但內容類型沒有改變,內容長度爲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" 

如果我改變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" 

我如何可以改變響應的Content-Type的任何想法?這可能是我的nginx或gunicorn配置的問題嗎?

感謝您的幫助!

回答

13

嘗試以下操作:

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

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

    return response 

一個快速提示,可以添加到您的NGINX配置的httpserver塊部分原因,這樣你就不必指定的觀點和其他的Django代碼中的編碼:

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

Matt,感謝您對nginx配置的提示!我一定會使用它。不幸的是,我已經嘗試了你的代碼建議,它返回一個411 Http錯誤(需要的長度)。嘗試在HttpResponse中將'length'或'Content-Length'的值指定爲參數導致Django錯誤(意外的關鍵字參數和關鍵字不能是表達式)。 – winter

+0

好的,我已經更新了我的答案。 – Matt

+0

'len(content)'應該是'len(content.encode('utf-8)' –

4

instructions from the docs它應該是這樣的:

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

希望這會有所幫助!

+0

Paulo,感謝您的幫助!事實證明,當我嘗試這種技術時,我收到了411長度要求錯誤。 !這與response ['Content-Length'] = len(content)結合起來很奇怪,因爲我沒有真正指定內容......除非response.write自動添加' test123'到響應的內容?無論如何,謝謝你的幫助! – winter

+0

它實際上是這樣的。文檔說_But如果你想增加內容,你可以使用響應作爲一個類似於文件的object_,所以'.write'將內容添加到響應:) –

4

所以這個工作對我來說:

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

謝謝大家,您的幫助!

+0

您不必爲內容指定任何內容,即response = HttpResponse(content_type ='image/png'),然後像你一樣寫入。它也很好地注意到你可以像length一樣獲得/設置content_type,response ['Content-Type'] – radtek