0

I am not trying to ask for free ride, but I don't seem to know how to do this at all.

My recent posts are about running jobs in the background, but I have no luck in doing that. So...

  1. User clicks run inside a form and it fires a job.
  2. It takes about 30 seconds to complete the job, returns, and tells Django view function to return HttpResponseRedirect(....).

So while the page is being redirect (it takes 30 seconds to signal "GO AHEAD").... I want to show user like an Ajax loading gif picture.

I don't have Ajax implemented and the system is way too complicated to hack on.

Can we actually do this with javascript? The problem is that it hasn't load any page yet because it needs heavy_work to finish.

result = heavy_work(....)
 .... more code ....
return HttpResponseRedirect(go to this page...)

Thanks!

user1012451
  • 3,343
  • 7
  • 29
  • 33

2 Answers2

0

Why don't you use a regular ajax call?

javascript

function do_heavy_lifting(argument) {
    $.ajax({
        type: 'GET',
        data: { argument: argument }, // if necesarry
        url: '/heavy_lifting_django_view_url/',
        beforeSend: function() {
            $('#loading').show();
        },
        success: function(data) {
            ...redirect...
        },
        cache: false
    });
}

html

<div id="loading" style="display:none">
<button onclick="do_heavy_lifting('argument');">
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
0

Using AJAX is simple: just show a 'loader' animated gif before the actual ajax call, and in 'on_success' response callback to hide the loader gif.

Bu without AJAX - the only solution so far is using iterators - look at this: How to stream an HttpResponse with Django

Community
  • 1
  • 1
Tisho
  • 8,320
  • 6
  • 44
  • 52