title: Django dynamic table with AJAX
body: <p>This is the second question in the row about my Django project. The code I use here has parts copied from this question:<a href="https://stackoverflow.com/questions/34774138/reload-table-data-in-django-without-refreshing-the-page?noredirect=1&amp;lq=1">Stack Overflow</a></p>
  
  <p><strong>What I aim to achieve</strong> is a dynamic table, that loops through objects in a list(<strong>currently the interval between records is 3 seconds</strong>). Let's say I have 21 records. The table first displays records of 1 to 10, on the displaykala.html table. Then it replaces only the table content with records of 11 to 20(with AJAX, without page refresh. The new contents come from get_more_tables_kala.html, and those table rows coming from there, are appended to the displaykala.html table).
  <strong>At no point should the table be empty</strong> (UNLESS there is simply no objects to display).</p>
  
  <p>Finally, the contents of 11-20 are replaced with contents of 21-30. In this case, only one row is displayed.</p>
  
  <p>Basicly it always displays first ten records(even if there is only one). Then the code increments the startindex and endindex of the rows, and checks if there is records between those. <strong>If</strong> there is more than 10 records, it will empty the table and instantly load up the next records (as long as there is even 1 record). <strong>Else</strong>, the program should wait X amount of seconds, until checking  again. </p>
  
  <p>The code has been going through various experimentations, I'm sorry for any dumb coding. I'm still learning Django and AJAX.</p>
  
  <p><strong>There is a few problems though, but let's focus on the main one</strong>
  When the displaykala.html is loaded, the table is displayed empty first, for 3 seconds. I know this is because the code loads the empty displaykala.html table.
  If I were to set in views that displaykala.html table should display rows 1 to 10 by default, I run into a rendering problem if there is more than 10 rows.</p>
  
  <p>Those rows are loaded from the get_more_tables_kala.html, but when the code is supposed to switch back to displaykala.html, I'm either forced to reload the page (which is not an option due to network traffic increase), or return a new render with displaykala.html as a parameter, which causes the page to create a "copy" of itself, in the place where the table rows are supposed to be.</p>
  
  <p><strong>What I want</strong> is the program to switch between the records, without diplaying empty page in between.</p>
  
  <p>I am open to any kind of optimizations or different ideas, as long as they're somewhat simple to understand. I know that this code is rubbish. I'm just trying to get it to work.</p>
  
  <p>I must have missed something vital to tell you, please comment below if you need more info.</p>
  
  <p><strong>EDIT:</strong> Also, when I look at the django server console, the three tables (1-10, 11-20(only two records) and the empty one) produce these rows:</p>
  
  <ul>
  <li>[14/Dec/2017 12:33:22] "GET /user/get_more_tables_k HTTP/1.1" 200 2222</li>
  <li>[14/Dec/2017 12:33:24] "GET /user/get_more_tables_k HTTP/1.1" 200 439</li>
  <li>[14/Dec/2017 12:33:27] "GET /user/get_more_tables_k HTTP/1.1" 200 1</li>
  </ul>
  
  <p>My code runs between <strong>views.py</strong>, <strong>js_kala.html</strong>,  <strong>displaykala.html</strong> and <strong>get_more_tables_kala.html</strong>.</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>from django.shortcuts import render, redirect
  from userside.models import Kala
  from django.contrib.auth.decorators import login_required
  from django.db import connection
  
  @login_required
  def displaykala(request):
      return render(request, 'displaykala.html')
  
  
  @login_required
  def get_more_tables_kala(request):
  
      startind = request.session.get('startind')
      if not startind:
          startind = 0
      request.session['startind'] = startind
  
      endind = request.session.get('endind')
      if not endind:
          endind = 10
      request.session['endind'] = endind
  
      kalat = Kala.objects.filter(rivinro__gt=startind, rivinro__lte=endind)
      count = kalat.count()
      if count == 0:
          request.session['startind'] = 0
          request.session['endind'] = 10
          kalat = Kala.objects.filter(rivinro__gt=startind, rivinro__lte=endind)
          return render(request, 'get_more_tables_kala.html', {'kalat': kalat})
      else:
          request.session['startind'] += 10
          request.session['endind'] += 10
          kalat = Kala.objects.filter(rivinro__gt=startind, rivinro__lte=endind)
          return render(request, 'get_more_tables_kala.html', {'kalat': kalat})
  </code></pre>
  
  <p><strong>js_kala.html</strong></p>
  
  <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt; &lt;/script&gt;
  &lt;script&gt;
      var append_increment = 0;
      setInterval(function() {
          $.ajax({
              type: "GET",
              url: "{% url 'get_more_tables_kala' %}",  // URL to your view that serves new info
              data: {'append_increment': append_increment}
          })
          .done(function(response) {
              $('#_appendHere_kala').html('');
              $('#_appendHere_kala').append(response);
              append_increment += 10;
          });
       }, 3000)
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>displaykala.html</strong></p>
  
  <pre><code>&lt;html&gt;
      &lt;META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"&gt;
    {% load static %}
      {% include 'loginbar.html' %}
      &lt;head&gt;
        &lt;link rel="stylesheet" type="text/css" href="{% get_static_prefix %}css/style.css"&gt;
          &lt;title&gt;Display&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
              &lt;h1&gt;Display&lt;/h1&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Rivinumero&lt;/th&gt;
                      &lt;th&gt;Tuote&lt;/th&gt;
                      &lt;th&gt;Latinankielinen nimi&lt;/th&gt;
                      &lt;th&gt;Pyyntialue&lt;/th&gt;
                      &lt;th&gt;Pyyntipäivä&lt;/th&gt;
                      &lt;th&gt;Tuotantotapa&lt;/th&gt;
                      &lt;th&gt;Pyydystyyppi&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;table id="_appendHere_kala" class="table table-striped table-condensed"&gt;
                          &lt;thead&gt;
                        &lt;/thead&gt;
                        {% include 'js_kala.html' %}
                          {% for kala in kalat %}
                          &lt;tr&gt;
                          &lt;tbody id="tbody"&gt;
                            &lt;td&gt;{{kala.rivinro}}&lt;/td&gt;
                            &lt;td&gt;{{kala.tuote}}&lt;/td&gt;
                            &lt;td&gt;{{kala.latinalainen_nimi}}&lt;/td&gt;
                            &lt;td&gt;{{kala.pyyntialue}}&lt;/td&gt;
                            &lt;td&gt;{{kala.pyyntipaiva|date:"d.m.Y" }}&lt;/td&gt;
                            &lt;td&gt;{{kala.tuotantotapa}}&lt;/td&gt;
                            &lt;td&gt;{{kala.pyydystyyppi}}&lt;/td&gt;
                          &lt;/tbody&gt;
                          &lt;/tr&gt;
                          {% endfor %}
                        &lt;/table&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>get_more_tables_kala.html</strong></p>
  
  <pre><code>{% load static %}
  {% for kala in kalat %}
  &lt;tbody id='tbody'&gt;
      &lt;tr&gt;
          &lt;td&gt;{{kala.rivinro}}&lt;/td&gt;
          &lt;td&gt;{{kala.tuote}}&lt;/td&gt;
          &lt;td&gt;{{kala.latinalainen_nimi}}&lt;/td&gt;
          &lt;td&gt;{{kala.pyyntialue}}&lt;/td&gt;
          &lt;td&gt;{{kala.pyyntipaiva|date:"d.m.Y" }}&lt;/td&gt;
          &lt;td&gt;{{kala.tuotantotapa}}&lt;/td&gt;
          &lt;td&gt;{{kala.pyydystyyppi}}&lt;/td&gt;
       &lt;/tr&gt;
  &lt;/tbody&gt;
  
  {% endfor %}
  </code></pre>