繁体   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