簡體   English   中英

正確的方法來更改默認的響應內容類型

[英]Proper way to change the default Response content-type

在Pyramid中,是否有一種更改默認Response內容類型的正確方法? 我發現我可以通過使用補間將內容類型從“text / html”更改為“application / xhtml + xml”來實現。

config.add_tween('app.tweens:XhtmlTween')
class XhtmlTween(object):

    def __init__(self, handler, registry):
        self.handler = handler
        self.registry = registry

    def __call__(self, request):
        # Process request.
        response = self.handler(request)

        # Change content-type.
        # - Taken from JSON.__call__ from <https://github.com/Pylons/pyramid/blob/master/pyramid/renderers.py>.
        if response.content_type == response.default_content_type:
            response.content_type = 'application/xhtml+xml'

        return response

然而,這看起來像一個可怕的黑客。 有一個更好的方法嗎?

從金字塔文檔, 實例化響應下的“請求和響應對象”:

您可以pyramid.response.Response並設置default_content_type以覆蓋此行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM