I would like to build with django-celery a web-server that:
- Accepts data from user on the page
/input/ - Starts a
Celerytask to process them - Redirects to the page
/wait/, where user waits until the processing is completed - When the task is completed, automatically redirects to the page
/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?
celerywill do this for you with the session object), pass the session id while redirecting (e.g.,return redirect('some-view-name', id=1234; alternatively, you can use cookies for this), and extract the required information from the database in the results view.