Prashant Gautam
Prashant Gautam

Reputation: 41

How to update template variable in Django

I want to update value of 'log_data_full' from django template, i have extracted updated value in json from AJAX, i dont' want to refresh the page . i just want to update table view with updated value. Is that possible?

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">                 
                <thead>
                  <tr>
                    <th>=Id</th>
                    <th>Category</th>
                    <th>Fee</th>
                    <th>Search</th>                     
                  </tr>
                </thead>                  
                <tbody>
                  {% for log in log_data_full %}
                  <tr>
                    <td>{{ log.Id }}</td>
                    <td>13</td>
                    <td>100</td>
                    <td>{{ log.search }}</td>                     
                  </tr>
                  {% endfor %}                               
                </tbody>
              </table>

AJAX query

$.ajax({                
                      url: "/filter",
                      type: "POST",                
                      data: {
                          from_date:from_date,
                          to_date:to_date,
                          csrfmiddlewaretoken: '{{ csrf_token }}',                                                  
                          },
                      success : function(json) {   
                              debugger                           
                              if(json != 'error'){
                                var res = JSON.parse(json);                               

                              }
                              else{
                                alert('Server Error! Could not get data');
                              }                                          
                      },
                      error : function(xhr,errmsg,err) {
                          alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
                      }
                  });

How to update value by of template variable 'log_data_full'?

Upvotes: 0

Views: 278

Answers (1)

Aarif
Aarif

Reputation: 1675

Assuming you want to show the updated data in the table received from the server.

in your success function

success : function(json) {
     if(json != 'error'){
        $("#dataTable").empty();    //remove existing data
        var res = JSON.parse(json);
        rowHtml = '';
        $.each(res, function (i, item) {
        rowHtml = '<tr><td>' + res.id + '</td><td>' + res.category + '</td><td>' + res.fee + '</td><td>'+ res.search +'</td></tr>';
            });
        $('#dataTable').append(rowHtml);
        }

Upvotes: 1

Related Questions