Currently I am building an app which update the main page's data via AJAX. Based on this question I used (see code/1/ below) ajax to refresh my page. My other aim is to catch just certain variable from the view and use as a JS variable. If I use only Django template variable in the AJAX request my variable not refreshig.
First attempt:
Django view:
def fetch_data (request):
query_1=Objects.all()
query_2=Objects.all()
#From these queries I colect data and push them to lists.
context= {
'variable_1': variable_1,
'variable_2': variable_2,
'time_variable': time_variable #changing in every minute
}
return render(request, 'app/index.html', context)
AJAX:
Here I use the AJAX request. I run it in every minute to realod my page my page. My problem is that I want to load just let's say the time_variable
to use it again within AJAX. If I use it like a single template variable ({{time_variale|safe}}
) when the AJAX reloading I get the same variable again and again and not a new one because of the refreshing. How gen I catch the Django variable?
setInterval(function(){
if(new Date().getSeconds() === 0) {
$.ajax({
url:'{% url 'fetch_data' %}',
success: function (data){
$('body').html(data); #This working, the data refreshing
var="{{time_variable|safe}}"; #Here I always get the same in every minutes despite the variable get new value in every minute in the view.
}
});
}
},1000)