2

Essentially, I am trying to do the same thing as the writer of this and this was trying to do. Which is to dynamically update the values of an html template in a django template via an ajax call.

Both of them managed to fix their issue, however, I am unable to do so after 1.5 days.

index.html

{% extends 'app/base.html' %} {%load staticfiles %} {% block body_block %} <div class="container" > {% if elements %} <table id="_appendHere" class="table table-bordered"> <tr> <th>A</th> <th>B</th> <th>C</th> <th>D</th> <th>User</th> <th>Date</th> </tr> {% for e in elements %} <tr> <td><a href="{{ e.A.url }}">{{e.A}}</a></td> <td><a href="{{ e.B.url }}">{{e.B}}</a></td> <td>{{ e.C }}</td> <td>{{ e.D }}</td> <td>{{ e.User }}</td> <td>{{ e.Date }}</td> </tr> {% endfor %} </table> {% else %} No data to display! <br/> {% endif %} </div> {% endblock %} <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; }); }, 1000) </script> 

views.py

def index(request): elements = ElementFile.objects.all().order_by('-upload_date') context_dict = {'elements':elements} response = render(request,'app/index.html',context_dict) return response def get_more_tables(request): increment = int(request.GET.get('append_increment')) increment_to = increment + 10 elements = ElementFile.objects.all().order_by('-upload_date')[increment:increment_to] return render(request, 'app/get_more_tables.html', {'elements': elements}) 

get_more_tables.html

{% for e in elements %} <tr> <td><a href="{{ e.A.url }}">{{e.A}}</a></td> <td><a href="{{ e.B.url }}">{{e.B}}</a></td> <td>{{ e.C }}</td> <td>{{ e.D }}</td> <td>{{ e.User }}</td> <td>{{ e.Date }}</td> </tr> {% endfor %} 

urls.py

 urlpatterns = [ path('', views.index, name=''), path('get_more_tables/', views.get_more_tables, name='get_more_tables'), ] 

in the javascript part, I tried: url: "{% url 'get_more_tables' %}", with and without quotes

If I try to access /get_more_tables/ I get the following error:

TypeError at /get_more_tables/

int() argument must be a string, a bytes-like object or a number, not 'NoneType'

So for some reason, I get nothing, the append_increment is the empty dictionary. But why? I tried altering the code like, but to no avail:

 try: increment = int(request.GET.get('append_increment')) except: increment = 0 

Expectation: dynamically loading database Outcome: error or non-dynamic loading (must refresh manually)

Million thanks to everyone who attempts to help with this.

10
  • 1
    request.GET is a query dict which works like this. >>> QueryDict('a=1&a=2&c=3') <QueryDict: {'a': ['1', '2'], 'c': ['3']}> docs.djangoproject.com/en/2.1/ref/request-response/… Commented Feb 16, 2019 at 13:22
  • 1
    as per the next error, (dynamic loading database) can you check if the migration for the model you are trying to access is successfully applied Commented Feb 16, 2019 at 13:24
  • 1
    1. it's "method":"GET" not "type". 2. you can check what request is going by checking network tab in chrome dev tools. Commented Feb 16, 2019 at 13:42
  • 1
    sorry for confusion. I was referring to javascript part of your code. where you wrote as { "type":"GET"}. api.jquery.com/jquery.ajax as to chrome dev tools, you should open network tab first and then reload the page to capture the outgoing traffic. Commented Feb 16, 2019 at 14:10
  • 1
    Let us continue this discussion in chat. Commented Feb 16, 2019 at 14:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.