I would like to build with django-celery a web-server that:
/input/Celery task to process them/wait/, where user waits until the processing is completed/result/Currently I got stuck with the code (simplified):
views.py:
def input(request):
# Read form data to 'parameters'
task = process_user_input.delay(parameters)
response = HttpResponseRedirect("/wait")
response.session['task'] = task
return response
def wait(request):
task = request.session.get('task', None)
while task.state not in ('SUCCESS', 'FAILURE'):
time.sleep(0.1)
return HttpResponseRedirect("/result")
def result(request):
#
urls.py:
urlpatterns = patterns('',
url(r'^input/$', views.input, name = 'input'),
url(r'^wait/$', views.wait, name = 'wait'),
url(r'^result/$', views.result, name = 'result'),
)
This, however, does not work, as 'HttpResponseRedirect' object has no attribute 'session'. I also tried to use render function, but it only loads corresponding template without calling the view function.
Can someone help me to fix the code so that it fulfils my goal?
After some research I came to conclusion I came to conclusion that the required functionality is not provided by django, as one cannot cannot render pages asynchronously (and, as a result, cannot render and redirect at the same time). Therefore, I decided to get rid of the /wait/ page. My views.py now looks like this:
from my_app.forms import InputForm
from mysite.tasks import process_user_input
def input(request):
form = InputForm()
# Read form data to 'parameters'
task = process_user_input.delay(parameters)
while task.state not in ('SUCCESS', 'FAILURE'):
time.sleep(0.1)
if task.failed():
# Insert error message and return to the input form
render(request, 'my_app/input.html', {'form': form})
return HttpResponseRedirect("/result")
def result(request):
#
The waiting message I had to implement using Javascript with jQuery in input.html:
<div id="form_container">
<!--The form (with id="input_form") is here-->
</div>
<div id="hidden_section" style="display: none;">
<!--The please-wait-message is here-->
</div>
<script>
$(document).ready(function () {
$("#input_form").submit(function () {
$("#form_container").attr("style", "display: none");
$("#hidden_section").attr("style", "display: block");
return true;
});
});
</script>
As a result, after submitting the form, a user remains on the /input/ page, but is seeing the please-wait-message until the task is completed or an error occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With