I have a Django project that i'm attempting to implement ajax on. The idea is that there are a list of alarms that are in a database (externally updated every 5 seconds or so).
Since The Database is automatically updated, what I'm trying to achieve is a page element that is refreshed every 10 seconds or so to display the new information (rather than refreshing the entire page). This already works with a page refresh or a meta refresh (eww..)
Here is what i have started so far, I've referenced the following Stackoverflow questions that i've used to try help me.
Refresh a div to shows new rating in Django using JQuery and AJAX
Refresh a div in Django using JQuery and AJAX
I've been going round in circles for a few days now without getting anywhere so any help is much appreciated.
index.html
<div id="right">
{% include 'alarmviewer/alarms.html' %}
</div>
alarms.html
Theres alot more in this one, but I didn't include it as it didn't seem necessary. It works without the ajax.
<div class="alarmsheader">.....</div>
urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^alarms/$', views.alarms, name='alarms'),
)
views.py
def alarms(request):
latest_alarm_list = ActiveAlarmsSimple.objects.order_by('severity')[:1000]
active_alarm_list = ActiveAlarmsCount.objects.order_by('-active_alarms')[:1]
return render_to_response('alarmviewer/alarms.html', {'latest_alarm_list': latest_alarm_list,'active_alarm_list': active_alarm_list,})
script.js
function refresh() {
$.ajax({
url: "{% url 'alarmviewer:alarms' %}",
success: function(data) {
$('#right').html(data);
}
});
};
$(document).ready(function ($) {
refresh();
var int = setInterval("refresh()", 3000);
Just a note. This all works fine without ajax implemented. refreshing the page every 5 seconds manually or with a meta tag works. I just get the error when running the server
[19/Jan/2015 11:38:22] "GET /alarmviewer/%7B%%20url%20%27alarmviewer:logical%27%20%%7D HTTP/1.1" 404 2936
Which I assume means there is something wrong with my views.py or my Urls.py as it does not seem to be able to find the alarms.html page it is meant to be refreshing.
Thanks for any help!
###"{% url 'alarmviewer:alarms' %}"###
and check what's between these hashes. – Ahmed Jan 18 '15 at 23:42