Archive for August, 2011

Returning XML file from django view

There are times when you want to return XML file from a template in Django. Check out the following:

In your view, you should have:

from django.http import HttpResponse
from django.template import loader, Context

def return_xml(request):
    t = loader.get_template("your_file.xml")
    c = Context( { "Message" : "Hello from XML"} )
    return HttpResponse(t.render(c), mimetype="text/xml")

And the following is your_file.xml

<?xml version="1.0" encoding="utf-8" ?>
<node>
    <sub_node>{{Message}}</sub_node>
</node>

Hope this helps

Previous month Next month