Reputation: 95
I'm not looking for exact code, but rather some guidance on my problem. On one of my pages, I want to have a small section that will tell the user the disk space left on the server and I want this information to update every 30 seconds. I have the code already written to get the disk information. However, I am unsure on how to display that information and have it update. This is what I have, just to give you a visual:
The HTML I'm working with: {% extends "Checklist/base.html" %}
{% block main_content %}
<form action="{% url 'Checklist:run' %}" method="post"> {% csrf_token %}
<input type="submit" name="submit_button" value="Run">
</form>
<label for="disk_space">Disk Space: </label>
<input type="text" name="disk_space" value="{{ disk_space }}" id="disk_space">
{% endblock %}
The Views Function for the HTML:
def submit(request): #start scrtipt
submit.preProcess = subprocess.Popen(['chmod', '+x /home/psvuser/Desktop/test.sh'])
submit.process = subprocess.Popen(['/home/psvuser/Desktop/test.sh'])
global sem
sem = True
disk_space = request.get('disk_space', '')
disk_space = diskSpace()
start_time= int(round(time.time()))
f = open("/home/psvuser/Desktop/writer.txt", "a")
f.write(str(start_time))
f.write(", ")
return render(request, 'Checklist/stop.html')
I have an idea on how to periodically refresh the page, however, I don't know how to display 'disk_space' onto the html.
Upvotes: 1
Views: 1205
Reputation: 2146
Since you want to do regular asynch calls to your server while a page is already loaded, I would use AJAX to solve this problem.
I'd usually do something like this
def your_django_view(request):
# magic
server_data = '98% left!'
response_data['data'] = server_data
return HttpResponse(json.dumps(response_data), content_type="application/json")
Then in your view, use some jquery (or javascript..whatever) to deal with all of the data
$.ajax({type: 'POST',
url: '/function_url/',
data: {
},
success: function(response_data) {
var server_mem = response_data['data']
$('#disk_space').val(server_mem);
})
Upvotes: 2
Reputation: 3730
Use this return statement at the end of your submit
view:
return render_to_response('Checklist/stop.html',
{'disk_space':disk_space},
context_instance=RequestContext(request))
The second argument of render_to_response
is a dictionary of {'key':value, 'key2':value2}
you can use in the template.
Upvotes: 0