Search code examples
xmldjangotemplatesdjango-templatesdetailview

Django DetailView/ListView force XML rendering


Is there a way to force DetailView/ListView to generate an XML file instead of an HTML?

I have already created an XML Template and it renders correctly, but the DetaiView doesn't allow to set the mimetype.

Any Ideas?

Thank you!


Solution

  • DetailView inherits from SingleObjectTemplateResponseMixin which inherits from TemplateResponseMixin which has a method render_to_response in which the mimemtype can be set. So, something like

    class MyView(DetailView):
        def render_to_response(self, context, **response_kwargs):
            return super(MyView, self).render_to_response(
                context,
                mimetype='application/xml',
                **response_kwargs)
    

    should work