Django:如何渲染 XML 文件,然后在同时渲染的视图中使用该 xml?
我想做的是在我的 Django 应用程序中使用 SIMILE 时间线。时间线需要 XML 文件中的数据。现在,我知道如何在 html 中渲染视图。我或许可以弄清楚如何将视图呈现为 XML。但是,如果磁盘上不存在 XML 文件(因为它是由 Django 生成的),那么如何渲染两者,然后将 XML 数据拉入 HTML 文件呢?
谢谢!
编辑:采用 XML 的行是用 Javascript 编写的,看起来像这样:
Timeline.loadXML("/static/example1.xml", function(xml,url) {eventSource.loadXML(xml,url); })
我需要一个路径,因为直接将 XML 作为字符串插入是行不通的。但不存在路径,因为 XML 文件实际上从未存在于磁盘上。
What I'm trying to do is use a SIMILE timeline in my Django app. The timeline requires it's data in an XML file. Now, I know how to render a view in html. And I can probably figure out how to render a view into XML. But how would one render both, then pull in the XML data to the HTML file if the XML file doesn't exist on disk (since it is being generated by Django)?
Thanks!
Edit: The line that takes the XML is in Javascript, and looks like this:
Timeline.loadXML("/static/example1.xml", function(xml,url) {eventSource.loadXML(xml,url); })
I need a path, since inserting the XML directly as a string doesn't work. But no path exists, since the XML file never actually exists on disk.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你似乎试图将太多的东西塞进同一个视图中。
我要做的如下:
@cache_page(60 * 60)
如果由于某种原因,您在生成 HTML 时需要 XML(正如您在标题中所示的那样),您可以直接调用从 HTML 视图中获取 XML 视图。例如:
当然,没有什么可以阻止您手动缓存到磁盘,但使用 Django 的工具会更容易。
You seem to be trying to cram too many things into the same view.
What I'd do is the following:
@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.:
Of course, there's nothing stopping you from manually caching to disk but it's easier to just use Django's facilities.
您不需要在视图中生成 XML。只需创建一个 XML 模板,将其渲染为字符串,并将结果写入临时文件。
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.