Django - show loading message during long processing

前端 未结 7 1422
野的像风
野的像风 2020-12-03 03:54

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

相关标签:
7条回答
  • 2020-12-03 04:10

    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.

    0 讨论(0)
  • 2020-12-03 04:14

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

    0 讨论(0)
  • 2020-12-03 04:15

    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)

    0 讨论(0)
  • 2020-12-03 04:18

    Here is another explanation on how to get a loading message for long loading Django views

    Views that do a lot of processing (e.g. complex queries with many objects, accessing 3rd party APIs) can take quite some time before the page is loaded and shown to the user in the browser. What happens is that all that processing is done on the server and Django is not able to serve the page before it is completed.

    The only way to show a show a loading message (e.g. a spinner gif) during the processing is to break up the current view into two views:

    1. First view renders the page with no processing and with the loading message

    2. The page includes a AJAX call to the 2nd view that does the actual processing. The result of the processing is displayed on the page once its done with AJAX / JavaScript

    0 讨论(0)
  • 2020-12-03 04:24

    A workaround that I chose was to use beforunload and unload events to show the loading image. This can be used with or without window.load. In my case, it's the view that is taking a great amount of time and not the page loading, hence I not using window.load.

    The downside is that there is a false message that goes out to the user that the page is loading even when when the request has not even reached the server or it's taking much time. And if, for some reason, the request dies out, the user continues to think that the page is loading, and loading. But I'm living with this for now.

    P.S. I cannot comment yet hence posted this as an answer.

    0 讨论(0)
  • 2020-12-03 04:28

    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.

    0 讨论(0)
提交回复
热议问题