views:

105

answers:

2

Hi. I have a view like this:

def form1(request):
    if request.method == 'POST':
        form = SyncJobForm(request.POST)
        if form.is_valid():
        # do something
        in_progress = True
        return render_to_response('form1.html', {'in_progress': in_progress})

I would like to know how to set it to refresh the template after is done with the view process. Like rendering the page after its done:

        in_progress = True
        return render_to_response('form1.html', {'in_progress': in_progress})
        # after its finished
        finished = True
        return render_to_response('form1.html', {'finished': finished})

How can I implement something like this? Thanks in advance.

A: 

I am not able to think of doing this without some sort of call back from the client (unless you use some asynchronous server mechanism, something I am not familiar with). One way would be to have the client poll the server periodically after receiving the "in_progress" notification to see if the processing has finished. The server side can be split into two calls, first to handle the POST as shown in your question and another one to find out and report if a given task is finished.

Manoj Govindan
A: 

You can't maintain state between page calls on a global basis, so you'll need to store your data in the database. In addition, a view can't negotiate anything with the browser after it has returned a page, so you need to split this into multiple views and spawn a separate thread for the job. Here's a general outline that might help:

def do_something():
    my_job = Jobs.get(id=blah)
    my_job.in_progress = True
    my_job.save()

    # Do some stuff here....

    my_job.in_progress = False
    my_job.save()

def step1(request):
    in_progress = Jobs.get(id=blah).in_progress
    if not in_progress:
        if request.method == 'POST':
            form = SyncJobForm(request.POST)
            if form.is_valid():
                thread.start_new_thread(do_something)
                return HttpResponseRedirect(step2)
            else:
                return render_to_response('form.html', 'form': form)
        else:
            form = SyncJobForm()
            return render_to_response('form.html', 'form': form)
     else:
         return HttpResponseRedirect(step2)

def step2(request):
    in_progress = Jobs.get(id=blah).in_progress
    if in_progress:
        return render_to_response('in_progress.html')
    else:
        return HttpResponseRedirect(finished)

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

Then have your page in_progress.html periodically refresh the page. When the job is completed, you can display a status message in finished.html.

There are more sophisticated ways to do this (write Javascript to poll the server periodically), but you're still going to need to write separate views to respond with the appropriate information. In addition, you could use a job management framework like Celery to create and execute jobs, but again you'll still have to create separate views to handle status information.

Daniel
Thanks. I have decided to go for this approach redirecting the user to another page once the form is submitted and a poll function to check the status.
Ping Pengũin