(Using Django) I'm creating a web application, this is how it's supposed to work :
- From the webpage - User selects an option, clicks on it
- The URL hits the associated view
- Inside the view function, another python module function start processing - which takes a while (could be 5 mins or 2 hours)
- The function execution logging is visible on the webpage as it happens - real time execution is visible on the webpage itself (asynchronously)
I found that, this can be achieved using AJAX. And I tried something like this:
views.py:
from lib.testnew import testprint
def printtest(request):
if 'create_btn' in request.POST:
test = testprint()
data = test.printing()
logger.info(data)
return render(request, 'create.html', {'data': data})
else:
logger.info("initiating")
return render(request, 'createvm.html')
create.html
{% extends 'base.html' %}
{% block content %}
<script type="text/javascript">
$(document).ready(function(){
$('#create_vm_btn').click(function(){
$.ajax({
type: "POST",
url: "printtest",
success: function(data) {
$("#output_id").html(data);
alert('Done: ' + data);
}
});
});
});
</script>
<h2>Welcome {{ user.first_name }} {{ user.last_name }}</h2>
<form method="post">
<textarea id="output" row=3 cols=25></textarea>
<button type="submit" class="btn btn-default" name="create_btn" id="create_btn">Create</button>
</form>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
{% endblock %}
The below line takes time to execute :
data = test.printing()
But it doesn't seem to have worked as I've explained in the points. Kindly suggest the right way.
But it doesn't seem to have worked as I want it to.and why you think we know how do you want?