ajax code
<button>Load Data</button>
<hr>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: "loadtabledata",
type: "GET", //send it through get method
data: { year_id : 1 },
success: function(response) {
$("#loadtable").html(response);
},
error: function(xhr) {
//Do Something to handle error
}
});
});
});
</script>
urls.py
path ('loadtabledata',views.loadtabledata,name='loadtabledata'),
class p_board_detail (models.Model):
board_year = models.ForeignKey(p_board_list,on_delete= models.CASCADE)
member_name = models.CharField(max_length=200)
member_post = models.CharField(max_length=200)
def __str__(self):
return "%s (%s) " % (self.board_year,self.member_post)
def loadtabledata(request):
year_id = request.GET.get('year_id')
if request.method == 'GET' :
post_id = p_board_detail.objects.filter(board_year_id= year_id)
return JsonResponse({'data': post_id})
this issue due to you need to serialize the data before send .We have to use method, but as you just started, you can go with the below approach,