title: Django template : how to auto-refresh a jQuery Datatables?
body: <p>I'm a new Django user, stuck on a problem. I'd like to obtain an auto-refresh of my jQuery Datatables datas, showing my new DB content without reload entire html page (using ajax request). I browsed a lot of posts, but did not find my graal...</p>
  
  <p>Here is my Django template.html :</p>
  
  <pre><code>{% block js %} 
  &lt;script type="text/javascript" language="javascript" class="init"&gt;
    $(document).ready(function(){
    var oTable = $('#db_name').DataTable();
    } );
  
    setInterval(function () {
        oTable.ajax.reload();
    }, 2000 );
  } );
  
  &lt;/script&gt; 
  {% endblock %}
  </code></pre>
  
  <p>My views.py :</p>
  
  <pre><code>def db_update(request):
      watch_files()
      all_ = db.objects.all()
      return render_to_response('supervision.html', {'all_fields': all_})
  </code></pre>
  
  <p>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 of the Datatables array.</p>
  
  <p>I tried also this, trying to pass JSON, but without success :</p>
  
  <pre><code>$(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_files()
      all_ = db.objects.all()
      jsondata = serializers.serialize('json', all_)
      return render_to_response('supervision.html', {
          'obj_as_json':json.dumps(jsondata), 
          'all_fields': all_})
  </code></pre>
  
  <p>If anybody could help me, it would be great. 
  Thks, Christophe</p>