Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render an xml to a view

The scenario goes like this.

I get an atom file from a website (say A). A third party will be request this atom file through my website (say B).

I am writing a Django app which will frequently poll website A and store it as a file. Now, when a third party requests for the file through website B, I will have to display the file as xml in the browser.

My question is how do I render an entire xml file to a view in Django?

 render_to_response

expects a template. I can't use a template as such. I just need to display the file in the view. How do I do this?

like image 556
vkris Avatar asked May 18 '11 14:05

vkris


People also ask

How do I visualize an XML file?

View an XML file in a browser If all you need to do is view the data in an XML file, you're in luck. Just about every browser can open an XML file. In Chrome, just open a new tab and drag the XML file over. Alternatively, right click on the XML file and hover over "Open with" then click "Chrome".

How do I display an XML note?

To view raw XML source, try to select "View Page Source" or "View Source" from the browser menu. Note: In Safari 5 (and earlier), only the element text will be displayed. To view the raw XML, you must right click the page and select "View Source".

How a XML document can be displayed on a browser?

You can add a stylesheet association processing instruction to an XML document that specifies a stylesheet for a web browser to use when displaying it. To display an XML document in a web browser: 1. You must first decide whether or not your XML document needs to reference a document type declaration (DTD).


2 Answers

Do something like the below:

return render(request, 'myapp/index.html', {"foo": "bar"}, content_type="application/xhtml+xml")
like image 60
silent1mezzo Avatar answered Sep 20 '22 03:09

silent1mezzo


Just define the MIME type to 'text/xml' using the content_type argument.

return render(request, 'xmltemplate.xml', {'foo':'bar'}, content_type='text/xml')

In the xmltemplate.xml render the variables if you want.

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <foo>{{ foo }}</foo>
</note>
like image 23
tinyhare Avatar answered Sep 20 '22 03:09

tinyhare