12

The code to get the data in my view looks like this:

order = Order.objects.filter(owner=request.user).order_by('-id')[:10]

The code in my template looks like this and works great. Now, the thing is that i want this table to update its information every 10 seconds without refreshing the whole page. The url to the view is / and the name of the template is home.

           <table class="table table-striped table-condensed">
              <tr>
                <th>Reg.nr.</th>
                <th>M&auml;rke</th>
                <th>Modell</th>
              </tr>
              {% for i in order %}

                {% if i.order_booked %}
                <tr class="success">
                {% else %}
                <tr>                    
                {% endif %}

                 <td>{{ i.regnr|upper }}</td>
                 <td>{{ i.brand|capfirst }}</td>
                 <td>{{ i.brand_model|capfirst }}</td>
              </tr>
              {% endfor %}
            </table>

urls.py looks like this

urlpatterns = [ url(r'^$',views.home, name='home'), url(r'^login/', auth_views.login, {'template_name':'login.html'}, name='account_login'), url(r'^logout/', auth_views.logout, {'template_name':'logout.html'},name='account_logout'), url(r'^add_order/', views.add_order, name='add_order'), url(r'^admin/', admin.site.urls), ]

Can anyone of you shed some light over this i would greatly appreciate it!

Likk0s
  • 153
  • 1
  • 1
  • 7
  • You need to use javascript. It's the only way to change the dom objects without refreshing the page. – Shang Wang Jan 13 '16 at 18:22
  • Ok, im fine with using javascript. But i have no idea of how it should be done, I have tried dozens of scripts to try to get it to work. Do you have any experience in js with django and now how to get it done? How do i get new data from the view without refreshing the whole page? – Likk0s Jan 13 '16 at 18:45
  • It's really hard to get somebody from stackoverflow to write code for you. Simple googling something like "django with javascript" would give you tons of results. I just randomly pick one that looks promising http://www.tangowithdjango.com/book/chapters/ajax.html – Shang Wang Jan 13 '16 at 19:08

1 Answers1

26

You can use setInterval, jQuery AJAX, and a view:

Your HTML

   <table id="_appendHere" class="table table-striped table-condensed">
      <tr>
        <th>Reg.nr.</th>
        <th>M&auml;rke</th>
        <th>Modell</th>
      </tr>
      {% for i in order %}
        {% if i.order_booked %}
        <tr class="success">
        {% else %}
        <tr>                    
        {% endif %}

         <td>{{ i.regnr|upper }}</td>
         <td>{{ i.brand|capfirst }}</td>
         <td>{{ i.brand_model|capfirst }}</td>
      </tr>
      {% endfor %}
    </table>

JS

<script>
    var append_increment = 0;
    setInterval(function() {
        $.ajax({
            type: "GET",
            url: {% url 'get_more_tables' %},  // URL to your view that serves new info
            data: {'append_increment': append_increment}
        })
        .done(function(response) {
            $('#_appendHere').append(response);
            append_increment += 10;
        });
    }, 10000)
</script>

View

def get_more_tables(request):
    increment = int(request.GET['append_increment'])
    increment_to = increment + 10
    order = Order.objects.filter(owner=request.user).order_by('-id')[increment:increment_to]
    return render(request, 'get_more_tables.html', {'order': order})

get_more_tables.html

  {% for i in order %}
  <tr>
    {% if i.order_booked %}
    <tr class="success">
    {% else %}
    <tr>                    
    {% endif %}

     <td>{{ i.regnr|upper }}</td>
     <td>{{ i.brand|capfirst }}</td>
     <td>{{ i.brand_model|capfirst }}</td>
  </tr>
  {% endfor %}

And then just make sure to add the new view into your urls.py (make sure it has name='get_more_tables', or whatever the name is in your JS url:)

What this does is use setInterval, a JS function, to make a GET AJAX request every 10000ms (10 seconds) to a view in your server. The view then renders a separate HTML file using this new context, and sends it as a response back to your original HTML page. The new response is then appended via jQuery to your table. Every time this cycle happens, the slice is incremented, so you don't get the same results from Order.

Keep in mind that this will loop non-stop, so if you want it to end you will have to add code to the JS to stop setInterval if the last response came back empty.

Fabs
  • 161
  • 1
  • 9
Hybrid
  • 6,741
  • 3
  • 25
  • 45
  • 1
    Thanks for your answer, I will try it out. It made me better understand what is needed. Thanks a lot. – Likk0s Jan 13 '16 at 20:15
  • No problem, if the solution works for you, be sure to click the green "check" mark to the left of my answer so that anyone who needs help will know the answer – Hybrid Jan 13 '16 at 20:17
  • Put your current urls.py in your original post and I will add it – Hybrid Jan 13 '16 at 20:43
  • 2
    Thanks for the code Hybrid, now it works like a charm. You really made my day, I have spent so many hours trying to work this out. – Likk0s Jan 13 '16 at 21:34