2012-08-03 72 views
0

有沒有什麼辦法可以提供模擬類型爲text/xml的flatpage?將模板更改爲具有不同文件擴展名的模板不起作用,並且我無法在文檔中找到任何可能的信息。服務XML Django flatpage?

回答

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