更改默认响应内容类型的正确方法

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

但是,这看起来像是一个糟糕的 hack。有更好的方法吗?

来自 Pyramid 文档,"Request and Response Objects" 在 Instantiating the Response 下:

You can subclass pyramid.response.Response and set default_content_type to override this behavior.