Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - show loading message during long processing

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.

like image 804
Evan Davey Avatar asked Nov 29 '11 20:11

Evan Davey


4 Answers

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)

like image 139
Evan Davey Avatar answered Nov 06 '22 16:11

Evan Davey


Another thing you could do is add a javascript function that displays a loading image before it actually calls the Django View.

function showLoaderOnClick(url) {
      showLoader();
      window.location=url;
  }
function showLoader(){
      $('body').append('<div style="" id="loadingDiv"><div class="loader">Loading...</div></div>');
  }

And then in your template you can do:

<a href='#' onclick="showLoaderOnClick('{% url 'my_namespace:my_view' %}')">This will take some time...</a>

Here's a quick default loadingDiv : https://stackoverflow.com/a/41730965/13476073 Note that this requires jQuery.

like image 36
anselm Avatar answered Nov 06 '22 16:11

anselm


a more straightforward approach is to generate a wait page with your gif etc. and then use the javascript

window.location.href = 'insert results view here';

to switch to the results view which starts your lengthy calculation. The page wont change until the calculation is finished. When it finishes, then the results page will be rendered.

like image 2
mysticvisionnnn Avatar answered Nov 06 '22 16:11

mysticvisionnnn


Here's an oldie, but might get you going in the right direction: http://djangosnippets.org/snippets/679/

like image 1
Brandon Avatar answered Nov 06 '22 16:11

Brandon