【发布时间】: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