[英]How to load data without blocking?
I want to load items from a database in my template. 我想从模板中的数据库加载项目。 This proccess of loading can take some seconds, so I want to display the page before the proccess is done. 此加载过程可能需要几秒钟,因此我想在过程完成之前显示页面。
{% for item in get_items() %}
<div> ... </div>
{% end %}
How can I load this data without blocking the page? 如何在不阻止页面的情况下加载此数据?
The easiest way would be two views: the view that's tied to your URL which just shows the "wrapper" around your actual data and has an HTML element in it like: 最简单的方法是两个视图:与您的URL绑定的视图,该视图仅显示实际数据周围的“包装”,并在其中包含一个HTML元素,例如:
<div id="my-content">(loading data)</div>
And, assuming you use jQuery, something like: 并且,假设您使用jQuery,则类似:
<script>
$("#my-content").load("/path/to/view/that/provides/data");
</script>
And then have a second view wired to that URL. 然后将第二个视图连接到该URL。 This should return just the formatted HTML of the data itself (so possibly just that table or however you're presenting it). 这应该只返回数据本身的格式化HTML(因此可能只返回该表,或者您要显示它)。
Of course, you can also do this not by having the second view provide formatted HTML; 当然,您也可以不通过让第二个视图提供格式化的HTML来做到这一点。 you could have it produce JSON or XML or some more data-y format and then format that with Javascript. 你可以有它产生的JSON或XML或更多的数据-Y格式,然后格式化使用Javascript。 For some cases, that can be more efficient. 在某些情况下,这可能更有效。
If you're not using jQuery, look up how to do a simple "fill this item with the response of this URL done via AJAX" call in whatever framework you do use--or you can do this in raw Javascript too, though somewhat more verbosely. 如果您不使用jQuery,请查找如何在使用的框架中进行简单的“通过AJAX完成此URL的响应来填充此项目”调用,或者也可以在原始Javascript中进行此操作,尽管有些更详细地说。
加载没有它们的页面,然后通过单独的Ajax调用将其添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.