[Answer]-Serving an XML Django flatpage?

1👍

id suggest to overwrite FlatpageFallbackMiddleware. you could set the response headers there like this:

#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

put your new middleware in settings.MIDDLEWARE_CLASSES and off your xml-file goes.

additional info about how to make a custom middleware can be found here

and here some info about setting response headers in django

👤Jakob

Leave a comment