Django update table on page every 5 sec

Viewed 41

So i have a template which gets a list of lists with information for table.

I render data like that way

Here is my template html

{% for each in info %}

                                                    <tr class="bg-dark-2">
                                                        <th data-label="Seller" scope="row"><span class="fw-bold">{{ each.2 }}</span></th>
                                                        <th data-label="U give">
                                                            <div class="d-flex align-items-center">
                                                                <span class="icon d-flex align-items-center justify-content-center me-3"></span><span>{{ each.4}}</span><span class="fw-normal ms-3">{{ each.0 }}</span>
                                                            </div>
                                                        </th>
                                                        <th data-label="U get"><span>{{ each.1 }} {{ each.5 }}</span></th>
                                                        <th data-label="Limit"><span>from {{ each.8 }} to {{ each.9 }}</span></th>
                                                        <th data-label="Reserve"><span>{{ each.6 }}</span></th>
                                                        <th data-label="">
                                                            <a class="btn btn-success btn-block" href="{{ each.3 }}">Buy</a>
                                                        </th>
                                                    </tr>
{% endfor %}

So the task is, how can i update data on my django webpage, without refreshing it? (every 5-10 seconds) Or how can i use ajax for delete table and download new data to it?

1 Answers

So. Problem solved by implementing ajax get request from Django view

I made up to ajax stuff.

  1. U need to create a new view in Django, in order to push updates.

  2. U have to make a ajax get request and grab all information from new template which will contain only table rows data.

U can do so by using this code:

<script>
$(document).ready(function () {
$("#payment").change(function () {
switch($(this).val()) {
case 'cash1':
$.ajax({
url : 'https://ursite.com/view_for_update_table_data',
type: 'GET',
dataType: 'html',
success: function(data){
$('#border1').html(data);
}
});
}
});
});
</script>
  1. Ajax will get all data from page you use for table-updates and than will replace all current rows with new ones from update view page.
Related