Views.py
def updatetable(request):
if request.method == 'GET' :
post_id = p_board_detail.objects.filter(board_year_id= year_id)
return HttpResponse("post_id")
here is the code for get data from db with the views function in django but not getting data....
Use JsonResponseData method instead of HttpResponse. After that success function in ajax. check console.log(data) whether its returning data or not.
- bhavana 5 years agoYou need to understand 2 things, whether your request is GET or POST
If its GET request then you need to handle the variable logic in your view and just return the data to AJAX
If you want to pass the data from AJAX then its POST request
year_id in views geting undefined variable ?? from django.http import JsonResponse is for jsonresponsedata
- sarojkoirala42 5 years agoyear_id in views geting undefined variable ?? from django.http import JsonResponse is for jsonresponsedata
- sarojkoirala42 5 years agodef updatetable(request): if request.method == 'GET' : post_id = p_board_detail.objects.filter(board_year_id= year_id) return JsonResponseData(post_id)
- sarojkoirala42 5 years ago<select name="bordlist"> <option class="mySelect">--Select Board Year--</option> {%for year in board_list %} <option value="{{year.id}}">{{year.board_year}}</option> {%endfor%} </select>
<hr>
<script>
$(document).ready(function(){
$(".mySelect").change(function(){
$.ajax(
{
type:"GET",
url: "{% url 'updatetable' %}",
data:{ year_id : $(this).val()},
success: function( data )
{
console.log(data) ;
$("#loadtable").html(data);
}
});
}) ;
});
</script>
<div class="table-responsive" >
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">S.N</th>
<th scope="col">Name</th>
<th scope="col">Post</th>
</tr>
</thead>
<tbody id="loadtable" >
{%for dt in data %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
<td>{{dt.name}}</td>
<td>{{dt.post}}</td>
</tr>
{%endfor%}
</tbody>
</table>
</div>
-
sarojkoirala42
5 years ago
sorry , I dont understand what is the issue ?.. are you taking about issue in views or AJAX?
- bhavana 5 years agocan you plz provide me the sample to get data on ajax views.py ajax method on html
- sarojkoirala42 5 years agocheck this
AJAX
$.ajax({
url: "ajax.aspx",
type: "get", //send it through get method
data: {
year_id: 4,
},
success: function(response) {
//Do Something
},
error: function(xhr) {
//Do Something to handle error
}
});
View
def updatetable(request,year_id):
if request.method == 'GET' :
post_id = p_board_detail.objects.filter(board_year_id= year_id)
return HttpResponse("post_id")
-
bhavana
5 years ago
are we getting dropdown event on change function calling in jquery? by looking on code
- sarojkoirala42 5 years ago$("#make").click(function(){ console.log("Saroj") ; }); on click it would print on console or not?
- sarojkoirala42 5 years agoLooks like your initial issue has been resolved.now it's new requirement you are looking for it. Can you pls close this ticket and submit new one
- bhavana 5 years agoI am stuck on the same probelm but trying to resolved ..thanks for. Ur support..
- sarojkoirala42 5 years agoAjax Get request issue is completed. Now you are looking for onclick event. Please close the ticket.
- bhavana 5 years ago
Acknowledged. May i know what is the issue?