Update div automatically in django's template

0

I am trying to develop a dashboard with django that shows notifications and updates its status automatically.

I was seeing that with Ajax and Jquery I can actulizar div without problem ...

But I'm having problems with the templates, so I use Django.

Here is my code

View of Django to show in loaddiv

@login_required
def profile(request):
    #email = Email.objects.all()
    squid_status = Squid.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('user', flat=True).distinct().count()
    email_status = Email_org.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('msgid', flat=True).distinct().count()
    incidencias_nav_status = Incidencia.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('id', flat=True).distinct().count()
    incidencias_mial_status = Incidencia_email.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('id', flat=True).distinct().count()
    incidencias_status = incidencias_nav_status + incidencias_mial_status
    return render(request, 'panel/loaddiv.html',{'email_status':email_status, 'squid_status':squid_status, 'incidencias_status':incidencias_status})

View of Django to show in dashboard template

@login_required
def profile2(request):
#email = Email.objects.all()
squid_status = Squid.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('user', flat=True).distinct().count()
email_status = Email_org.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('msgid', flat=True).distinct().count()
#incidencias_nav_status = Incidencia.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('id', flat=True).distinct().count()
#incidencias_mial_status = Incidencia_email.objects.filter(fecha__gte=timezone.now() - timezone.timedelta(days=1)).values_list('id', flat=True).distinct().count()
return render(request, 'panel/profile2.html')

Javascript in the template that shows all the information

<script>
  $(document).ready(function() {
  var refreshId =  setInterval( function(){
  $('#alerta_incidencias').load('/loaddiv');//actualizas el div
  }, 1000 );
  });
</script>

HTML on the page where the data is updated

<div id="alerta_incidencias">
 <div class="widget red-bg p-lg text-center">
 <div class="m-b-md">
 <i class="fa fa-bell fa-4x"></i>
 <h1 class="m-xs">{{incidencias_status}} </h1>
 <h3 class="font-bold no-margins">
  Incidencia{{incidencias_status|pluralize}}  
  registrada{{incidencias_status|pluralize}}
 </h3>
 <h4 class="font-bold no-margins"><a  href="{%url 'incidencias' %}">          
 Detalles...</a></h4>
 </div>
 </div>

I can not get the template loading the data from "loaddiv" To my knowledge I am passing the parameters of the url in the javascript wrong

Greetings

    
asked by elMor3no 18.04.2017 в 19:02
source

2 answers

3

First, the use of profile and profile2 views is not clear. In addition, you should clarify in your message that the html shown corresponds to loaddiv (which you clarified in the comments, but since you edited the question, preferable if you make the question better).

Original interpretation of your program:

Now, regarding the problem, I understand that javascript generates a request to the server for the resource called loaddiv , and then your server should respond executing a view, if there is a path to < strong> loaddiv in urls.py. I suppose that profile2 responds to the call of loaddiv, but the result of executing profile2 is the result of the render method, what does the render method do, here is indicated by the documentation: it returns an HttpResponse object, resulting from combining the content of the indicated html with the data of the sent context (you are not sending anything).

New interpretation of your program:

Calling loaddiv executes the profile view, which returns an HttpResponse object with the data obtained by the instructions contained in it. Now the question is: what is the relationship between the files loaddiv.html and profile2.html ?, where is the javascript located?

Normally I do not use this way of making calls between client and server, and I consider that it has a defect: what happens if HttpResponse returns an unexpected result ?, because that unexpected result will be loaded in the div that you are updating.

    
answered by 22.04.2017 в 18:07
0

Reading and debugging the code I found that all the problem was coming because I was loading the jquery at the end of the code instead of the hearder ...

By solving this, everything works perfectly for me ... Greetings and thank you very much ...

    
answered by 25.04.2017 в 21:18