Is there any way to serve a flatpage with a mimetype of text/xml
? Changing the template to one with a different file extension doesn't work, and I can't find any info in the documentation that says this is possible.
Asked
Active
Viewed 155 times
0

Randall Ma
- 10,486
- 9
- 37
- 45
1 Answers
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
- 778
- 3
- 9
- 25