2

I am constructing an application that need a lengthy calculation. After a user submitted the information, it need about 30 minutes to calculate and then return the result. So I am considering to add a “please wait" page.

I followed instructions mentioned in the following link, http://groups.google.com/group/django-users/browse_thread/thread/c1b0d916bbf86868 However, when I submit something, it stays in http://127.0.0.1:8000/please_wait and will not redirect to the result page like http://127.0.0.1:8000/display_DHM

does anybody know what is going on?

Here are all related files, I tried various ways, but when I submit a form, it only return the please_wait page and then stay there forever. There is no redirect happened. Since I want to check if it works first, there is no actual calculation in the code.

url.py

urlpatterns = patterns('',
       (r'^test$',views.test_form),
       (r'^please_wait', views.please_wait),
   url(r'^run_DHM$', views.run_DHM, name="run_DHM") ,
   url(r'^displayDHM', views.display_DHM, name="displayDHM")
)

view.py

def test_form(request):
       return render_to_response('test.html')

def please_wait(request):
       return render_to_response('please_wait.html')

def run_DHM(request):
      ### lengthy calculations... ...
       return HttpResponse("OK")

def display_DHM(request):
   return render_to_response('display_DHM.html')

test.html

{% extends "baseFrame.html" %}

{% block maincontent %}
 <form method="POST" action="please_wait">
  <p>Test:</p>
  <div id="address"></div>
  <p>Type your value in here:</p>
  <p><textarea name="order" rows="6" cols="50" id="order"></
textarea></p>
  <p><input type="submit" value="submit" id="submit" /></p>
 </form>
{% endblock %}

please_wait.html

<html>Please wait
<script type="text/javascript" src="http://code.jquery.com/
jquery-1.7.1.min.js">
$.getJSON('{% url run_DHM %}', function(data) {
       if (data == 'OK') {
                 window.location.href = '{% url displayDHM %}';
           } else {
                 alert(data);
           }
   });
</script>
</html>

display_DHM.html

<HTML>
<BODY>END FINALLY!</BODY>
</HTML>
Ted Kaplan
  • 464
  • 2
  • 8
user1098739
  • 21
  • 1
  • 2
  • You should move this process to background, enqueue it or something like that. Celery is the right way to do this. Also you can use threads. – dani herrera Dec 14 '11 at 22:13

2 Answers2

1

I write here because I can't use the comment space. My question is a bit similar to yours and maybe the answer can help you.

Briefly:

the question:

I have an external python program, named c.py, which "counts" up to 20 seconds. I call it from my Django app views.py and in the html page I have a button to start it. It's ok (= in Eclipse I can see that c.py prints 0,1,2,3,...20 when I press the button on the webpage) but I would like that the button changes from "GO" to "WAIT" during c.py process (or I would like to perform a waiting page during the counting or also a pop-up).

the answer:

You would need to be able to report back the status of c to the client via ajax long polling or WebSockets, or, if you don't care about the incremental status of c and just want to change the text of the link, you'll need to use JavaScript to set the value when the click event of the link fires:

views.py

from django.core.urlresolvers import reverse
from django.http import JsonResponse

def conta(request):
    c.prova(0)
    redirect = reverse('name_of_home_user_view')
    return JsonResponse({'redirect': redirect})

and js:

// assuming jQuery for brevity...

$(document).ready(function() {

    // avoid hard-coding urls...
    var yourApp = {
        contaUrl: "{% url 'conta' %}"
    };

    $('#btnGo').click(function(e) {
        e.preventDefault();  // prevent the link from navigating

        // set css classes and text of button
        $(this)
            .removeClass('btn-primary')
            .addClass('btn-danger')
            .text('WAIT');

        $.get(yourApp.contaUrl, function(json) {
             window.top = json.redirect;
        });
    });
});
Community
  • 1
  • 1
Trix
  • 587
  • 1
  • 6
  • 27
  • If you have a new question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. – Maxime Rouiller Mar 17 '15 at 12:48
  • @Maxime Rouiller: I have not sufficient reputation to comment so I posted an answer with my question not with the aim to obtain more answers but because I think that its answer can be useful for this question. I did not right? – Trix Mar 17 '15 at 13:30
  • After review, yeah. It's kind of a failure of the system. But after viewing the extensive answer that you gave, it would not have fit in a comment anyway. I recommend you add a few nice answers like this and earn the 50 reputation needed to comment. Added an upvote to you. – Maxime Rouiller Mar 17 '15 at 17:04
0

If you need lengthy calculations I think you could be interested in celery. Nice waiting pages (progress indicators?) would be a byproduct.

tback
  • 11,138
  • 7
  • 47
  • 71