I have been facing a challenge trying to display data from a celery task in a separate window. My expertise in JavaScript
and AJAX
is limited, which is where I am encountering issues. Once a view is executed, the celery task commences and the subsequent HTML page (success.html
) is displayed:
success.html
{% block content %}
<body>
{% if task_id %}
<h1>task_id has been called: {{ task_id }}</h1>
<script src="{% static 'MyAPP/bootstrap/js/task_output_retrieval.js' %}"></script>
<script type='text/javascript'> task_state("{{ task_id }}"); </script>
<script src="{% static 'MyAPP/bootstrap/js/update-hello-user.js' %}"></script>
<script type='text/javascript'> second(); </script>
<h1> END </h1>
{% endif %}
</body>
{% endblock content %}
Although I can confirm that the JavaScript
is being invoked as a new window appears, I'm now sharing the contents of my .js file:
task_output_retrieval.js
function task_state (task_id) {
var taskID = task_id;
var newWin = window.open('', 'new window', 'width=200, height=100');
$.ajax({
url: '{% url validate_task_state %}',
data: {'taskID':taskID},
method: 'POST',
dataType : "json",
success: function(data){
$(newWin.document.body).html(data);
newWin.document.write(data);
newWin.document.close();
newWin.focus();
newWin.print();
newWin.close();
},
error: function (){ alert('An error occured'); }
});
}
task_state(task_id);
As for the urls.py:
url.py
url(r'^ajax/task_state/$', task_state, name='validate_task_state'), # for ajax
And from the view:
admin_scripts.py
def task_state(request):
print ("You reached the task_state function")
data = 'Fail'
task_id = request.GET.get('task_id')
try:
async_result = AsyncResult(task_id)
except KeyError:
ret = {'error':'No optimisation (or you may have disabled cookies).'}
return HttpResponse(json.dumps(ret))
print ("request.is_ajax(): {0}".format(request.is_ajax()))
if request.is_ajax():
if 'task_id' in request.POST.keys() and request.POST['task_id']:
task_id = request.POST['task_id']
async_result.get()
data = {
'state': async_result.state,
'result': async_result.result,
}
else:
data = 'No task_id in the request'
else:
raise SuspiciousOperation("This is not an ajax request.")
json_data = json.dumps(data)
return HttpResponse(json_data, content_type='application/json')
Despite grappling with unresolved issues within the task_state, I am determined to navigate through this using trial and error. Currently, I'm experiencing difficulties in executing the task_state. The root cause seems to be linked to the AJAX call ('url'), but the exact problem remains elusive. Can someone help identify my mistake?
Update: Upon selecting the "JS Test Stuff" checkbox, the success.html
page renders without errors. Both JavaScript files, including update-hello-user.js
, are successfully called from success.html
. Although the window generated by task_output_retrieval.js
pops up, the view isn't triggered:
$.ajax({
url: query_url,
)
This specific section doesn't get rendered. In the console output, the following messages are logged:
[17/Aug/2018 04:59:12] INFO [django.server:124] "GET /MyApp/opt/ HTTP/1.1" 200 6631
async_result f2224e67-3e47-4980-9dc8-58622928e090
TASK_ID f2224e67-3e47-4980-9dc8-58622928e090
[17/Aug/2018 04:59:14] INFO [django.server:124] "POST /MyApp/opt/ HTTP/1.1" 200 6412
[17/Aug/2018 04:59:14] INFO [django.server:124] "GET /MyAppsite-static/MyApp/bootstrap/js/update-hello-user.js HTTP/1.1" 200 52
[17/Aug/2018 04:59:14] INFO [django.server:124] "GET /MyAppsite-static/MyApp/bootstrap/js/task_output_retrieval.js HTTP/1.1" 200 640