[Answered ]-Django: how do I render an XML file, then use that xml in a simultaneously rendered view?


You seem to be trying to cram too many things into the same view.

What I’d do is the following:

  1. Create a view that generates the XML (every request to the view should generate the XML from scratch).
  2. Create a view that uses the timeline widget and points it to the XML in 1)
  3. Enable Django’s caching layer and annotate the XML view appropriately. E.g., @cache_page(60 * 60)

If, for some reason, you need the XML at the time of generation of the HTML (as you seem to indicate in your title), you can just directly call your XML view from the HTML one. E.g.:

def xml(request):
  # ... generate xml

def html(request):
  xml = xml(request)
  # ... generate timeline from xml

Of course, there’s nothing stopping you from manually caching to disk but it’s easier to just use Django’s facilities.



You don’t need to generate your XML in a view. Just create an XML template, render it to string, and write the result to a temp file.

Leave a comment