2010-08-08 33 views
5

你好,我希望我的內容有一個純文本版本。所以我有一個單獨的模板。我打電話render_to_responsemimetype="text/plain"但我想告訴瀏覽器在HTTP響應中打開該頁面的內容是utf-8編碼。我該怎麼做(例如,我必須添加到render_to_response)?在django發送content-encoding標頭

回答

7

只需添加字符集到MIME類型是這樣的:

mimetype="text/html; charset=utf-8" 

真正發生的場景背後是什麼MIME類型取出kwargs在render_to_response

httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} 
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) 

並送到HttpResponse它並將它設置爲content_type

if mimetype: 
    content_type = mimetype  # For backwards compatibility 
if not content_type: 
    content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, 
       settings.DEFAULT_CHARSET) 
+0

在Django中的參數被稱爲CON​​TENT_TYPE的新版本insted的MIME類型的。 – 2016-01-14 09:35:44