Django - 在长时间处理期间显示加载消息 [英] Django - show loading message during long processing

查看:198
本文介绍了Django - 在长时间处理期间显示加载消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个Django视图需要花费大量时间才能执行大数据集上的计算。



当进程加载时,我想向用户介绍一个反馈消息,例如:旋转加载动画gif或类似的。

解决方案

在尝试了Brandon和Murat提出的两种不同方法后,Brandon的建议证明是最成功的。


  1. 创建包含 http://djangosnippets.org/片段/ 679 / 。 JavaScript已被修改:(i)在没有表单的情况下工作(ii)在返回完成标志时隐藏进度条/显示结果(iii)JSON更新url指向下面描述的视图


  2. 将慢加载功能移动到线程。该线程将被传递一个缓存密钥,并负责更新具有进度状态的高速缓存,然后是其结果。线程将原始模板作为字符串呈现,并将其保存到缓存。


  3. 根据 http://djangosnippets.org/snippets/678/ 修改为(i)而是渲染原始的包装器模板,如果progress_id =''(ii)生成cache_key,检查缓存是否已经存在,如果没有启动新线程(iii)监视线程进度,完成后将结果传递给包装器模板


  4. 包装器模板通过 document.getElementById('main')显示结果innerHTML = data.result


(*看看步骤4是否可以通过重定向更好地实现,因为渲染的模板包含当前由文档未运行的javascript。 getElementById('main')。innerHTML = data.result


How can I show a please wait loading message from a django view?

I have a Django view that takes significant time to perform calculations on a large dataset.

While the process loads, I would like to present the user with a feedback message e.g.: spinning loading animated gif or similar.

解决方案

After trying the two different approaches suggested by Brandon and Murat, Brandon's suggestion proved the most successful.

  1. Create a wrapper template that includes the javascript from http://djangosnippets.org/snippets/679/. The javascript has been modified: (i) to work without a form (ii) to hide the progress bar / display results when a 'done' flag is returned (iii) with the JSON update url pointing to the view described below

  2. Move the slow loading function to a thread. This thread will be passed a cache key and will be responsible for updating the cache with progress status and then its results. The thread renders the original template as a string and saves it to the cache.

  3. Create a view based on upload_progress from http://djangosnippets.org/snippets/678/ modified to (i) instead render the original wrapper template if progress_id='' (ii) generate the cache_key, check if a cache already exists and if not start a new thread (iii) monitor the progress of the thread and when done, pass the results to the wrapper template

  4. The wrapper template displays the results via document.getElementById('main').innerHTML=data.result

(* looking at whether step 4 might be better implemented via a redirect as the rendered template contains javascript that is not currently run by document.getElementById('main').innerHTML=data.result)

这篇关于Django - 在长时间处理期间显示加载消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆