提供XML Django平面页面?

时间:2012-08-03 18:28:33

标签: xml django django-flatpages

有没有办法提供mimetype为text/xml的平面页?将模板更改为具有不同文件扩展名的模板不起作用,我在文档中找不到任何可能的信息。

1 个答案:

答案 0 :(得分:1)

id建议覆盖FlatpageFallbackMiddleware。您可以像这样设置响应头:

#myproject/middleware.py
from django.contrib.flatpages.middleware import FlatpageFallbackMiddleware

class XmlFlatpageFallbackMiddleware(FlatpageFallbackMiddleware):
    def process_response(self, request, response):
        if response.status_code != 404:
            return response # No need to check for a flatpage for non-404 responses.


        response = super(XmlFlatpageFallbackMiddleware, self).process_response(request, response)
        # this depends on your settings.APPEND_SLASH
        # see django.contrib.flatpages.views.flatpage for details
        if request.path_info.endswith('.xml') or request.path_info.endswith('.xml/'): 
            response['Content-Type'] = 'text/xml; charset=utf-8'
        return response

将您的新中间件放在 settings.MIDDLEWARE_CLASSES 中,然后关闭您的xml文件。

可以找到有关如何制作自定义中间件的其他信息here

这里有一些info about setting response headers in django