Django and Auto-refresh Ajax

Django and Auto-refresh Ajax

liblibliblib Posts: 1Questions: 1Answers: 0

Hello,

I'm a new Django user, stuck on a problem. I'd like to obtain an auto-refresh of my Datatables datas, showing my new DB content without reload entire html page. I browsed a lot of posts, but did not find my graal...

My template.html :

$(document).ready(function(){

  var oTable = $('#db_name').DataTable();
  } );
  setInterval(function () {
      oTable.ajax.reload();
  }, 2000 );

} );

My views.py:

def db_update(request):

watch_file()
all_ = db.objects.all()
return render_to_response('supervision.html', {'all_fields': all_})

The problem is an error message "Datatables warning: table id=db_name - Invalid JSON response" displayed each 2sec. I think this is normal because no JSON. Despite this error, the reload is effective : if I do a manual refresh (F5), all new datas added in my DB (the function watch_files create entries in it) appear well in my page.
The ideal, for me, would be to obtain a transparent datas auto-refresh, keeping current sort/page options.

I tried also this, trying to pass JSON, but without success :

$(document).ready(function() {

  var oTable = $('#db_name').DataTable( {
      ajax: {{obj_as_json}}
  } ); 
  setInterval(function () {
      /* oTable.ajax.reload(); */
      oTable.fnReloadAjax();
      /* oTable.fnDraw(); */
  }, 2000 );

} );

def db_update(request):

watch_file()
all_ = db.objects.all()
jsondata = serializers.serialize('json', all_)
return render_to_response('supervision.html', {'obj_as_json': json.dumps(jsondata), 'all_fields': all_})

If anybody could help me, it would be great.
Thks, Christophe

This discussion has been closed.