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

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

问题描述

如何从 django 视图显示请稍候加载消息?

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

我有一个 Django 视图,需要花费大量时间对大型数据集执行计算.

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

在加载过程中,我想向用户显示反馈信息,例如:旋转加载动画 gif 或类似内容.

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

推荐答案

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

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

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

  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

将慢速加载功能移至线程.该线程将被传递一个缓存键,并将负责更新缓存的进度状态及其结果.线程将原始模板呈现为字符串并将其保存到缓存中.

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.

基于来自http://djangosnippets.org/snippets/678/<的upload_progress创建视图/a> 修改为 (i) 如果 progress_id='' (ii) 生成 cache_key,检查缓存是否已经存在,如果没有启动新线程 (iii) 监视线程的进度以及何时完成,将结果传递给包装模板

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

包装模板通过 document.getElementById('main').innerHTML=data.result

(* 考虑是否可以通过重定向更好地实现第 4 步,因为呈现的模板包含当前未由 document.getElementById('main') 运行的 javascript.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天全站免登陆