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.

mrg
  • 53
  • 1
  • 10
  • In case it made any difference, three of the values are accessed via another object, e.g. `{{ e.D }}` should be `{{ e.someObject.D }}` – mrg Feb 16 '19 at 13:14
  • 1
    request.GET is a query dict which works like this. >>> QueryDict('a=1&a=2&c=3') https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.QueryDict.__init__ – Krishna Chaitanya Kornepati Feb 16 '19 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 – Krishna Chaitanya Kornepati Feb 16 '19 at 13:24
  • Thanks Krishna. Tried the migrations, but there were no changes. :( If I print request.GET, I get {}, the empty dictionary. What am I doing wrong in your opinion? – mrg Feb 16 '19 at 13:35
  • 1
    1. it's "method":"GET" not "type". 2. you can check what request is going by checking network tab in chrome dev tools. – Krishna Chaitanya Kornepati Feb 16 '19 at 13:42
  • thanks for your response again. 1. sorry, not sure what you mean. just before you said request.GET is a query dict. now you refer to it as a method? and what does not "type" mean? 2. everything is empty in the Network tab – mrg Feb 16 '19 at 14:04
  • 1
    sorry for confusion. I was referring to javascript part of your code. where you wrote as { "type":"GET"}. http://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. – Krishna Chaitanya Kornepati Feb 16 '19 at 14:10
  • okay, I see now, thanks. the linked two examples on stackoverflow had type , but I changed it to method - no change in the running of the app In the network tab, if I reload index.html I just see the page and the loaded css and js files (all with status 200) if I reload get_more_tables.html, I just the the page itself with 200 – mrg Feb 16 '19 at 14:17
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188520/discussion-between-krishna-chaitanya-kornepati-and-mrg). – Krishna Chaitanya Kornepati Feb 16 '19 at 14:19
  • Thanks, Krishna, managed to get it working. To the others wondering, url: "{% url 'get_more_tables' %}" this must be just url: "get_more_tables/" in the js file – mrg Feb 16 '19 at 23:55

0 Answers0