Saturday, February 22, 2020

Django- ajax passing multiple values to the url

I am trying to get more details on clicking a button "type="button" class="apireq"" defined on each row and capture the result in javascript and show it in a window 
I am not able to get the clicked row data in jquery .It says variable value undefined .I wanted to POST multiple values . Is this the way to do it ? Do I need to modify urls.py 

template
----------
<tbody id="collist"></tbody>
  </tr>   
    {% for index, row in s_results.iterrows %} 
<tr>
  <td> {{ row.colname }} </td> <td> {{ row.dbname }} </td> <td> {{ row.schemaname }} </td> <td> {{ row.tablenm }} </td>
          <td> {{ row.coltype }} </td> <td> {{ row.collength }} </td> <td> {{ row.colscale }} </td>      
          <td>  <input type="button" class="apireq" 
data-colname="{{row.colname}}"
data-dbname="{{row.dbname}}"
data-schemaname="{{row.schemaname}}"
data-tablenm="{{row.tablenm}}" /> </td>
  <tr>
    {% endfor %}
</tbody>
  
js/app.js
----------
$('.apireq').click( function() {
    var jq_column_name=  $("#collist tr").last().attr("data-colname");
    var jq_dbname=  $("#collist tr").last().attr("data-dbname");
    var jq_schemaname=  $("#collist tr").last().attr("data-schemaname");
    var jq_tablenm=  $("#collist tr").last().attr("data-tablenm");
    $.ajax({            
             type: "POST",
              url : "http://localhost:8000/refresh1/",
              contentType: 'application/json; charset=utf-8',
              data: { column_name:jq_column_name,db_name:jq_dbname,schema_name:jq_schemaname,table_name:jq_tablenm },
             success : function (data) {
                      alert("success!!"); 
                    },
              error : alert("Failed!!")
                 });                 
});

urls.py
-------
path('refresh/<str:column_name>/<str:db_name>/<str:schema_name>/<str:table_name>', views.refresh1, name='refresh1'),

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/04e7e9be-1748-4ccc-9ef0-3ee06f22b9ce%40googlegroups.com.

No comments:

Post a Comment