Display xml for presentation

The scenario is as follows.

I get the atom file from the website (say A). A third party will request this atom file through my site (say B).

I am writing a Django application that will often try website A and save it as a file. Now that a third party is requesting a file through website B, I will have to display the file as xml in a browser.

My question is how to make the whole XML file representation in Django?

render_to_response 

awaiting template. I can not use the template as such. I just need to display the file in the view. How to do it?

+6
source share
3 answers

If you do not want to display the template, do not do this. render is just a shortcut to create a template. If you just want to display the text, just pass it to the HttpResponse.

Since your data is in a file, this will work:

 return HttpResponse(open('myxmlfile.xml').read()) 

although you should beware of concurrency problems if more than one person connects to your site at the same time.

+2
source

Do something like that.

 return render(request, 'myapp/index.html', {"foo": "bar"} content_type="application/xhtml+xml") 
+12
source

You just need to determine the MIME type to 'text/xml' with the content_type argument:

 return HttpResponse(open('myxmlfile.xml').read(), content_type='text/xml') 
+6
source

Source: https://habr.com/ru/post/888428/


All Articles