简体   繁体   English

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

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

In Pyramid, is there a proper way to change the default Response content-type? 在Pyramid中,是否有一种更改默认Response内容类型的正确方法? I figured out I can do it by using a tween to change the content-type from 'text/html' to 'application/xhtml+xml'. 我发现我可以通过使用补间将内容类型从“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

However, this looks like an awful hack. 然而,这看起来像一个可怕的黑客。 Is there a better way to do this? 有一个更好的方法吗?

From the Pyramid docs, "Request and Response Objects" under Instantiating the Response : 从金字塔文档, 实例化响应下的“请求和响应对象”:

You can subclass pyramid.response.Response and set default_content_type to override this behavior. 您可以pyramid.response.Response并设置default_content_type以覆盖此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM