【问题标题】:Proper way to change the default Response content-type更改默认响应内容类型的正确方法
【发布时间】:2018-09-25 13:18:27
【问题描述】:

在 Pyramid 中,是否有适当的方法来更改默认的响应内容类型?我发现我可以通过使用补间将内容类型从“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。有没有更好的方法来做到这一点?

【问题讨论】:

    标签: python python-3.x pyramid


    【解决方案1】:

    来自 Pyramid 文档,Instantiating the Response 下的“请求和响应对象”:

    您可以继承 pyramid.response.Response 并设置 default_content_type 以覆盖此行为。

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2011-08-27
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多