I have a booleanField in my hostelApplication model, I want to change that field using html button, but I don't know much about js. Please anyone tell me what I am doing wrong. here is my models.py, I have shown only necessary fieds.
class hostelApplication(models.Model):
hostelName = models.CharField(verbose_name='Select Hostel', max_length=20, choices=hostels, default='shivalik')
isApproved = models.BooleanField(default=False)
def __str__(self):
return self.hostelName
I have added my url path in urls as
url(r'^ajax/change_status/$', views.ajax_change_status, name='ajax_change_status')
here is my html file
{% for application in applications %}
{% csrf_token %}
<div class="table-content">
<div class="table-row">
<div class="table-data">{{application.hostelName}}</div>
<div class="table-data">{{application.dateApplied}}</div>
<div class="table-data">
<button type="button" class="btn btn-info" id="state">Approve</button>
</div>
</div>
{% endfor %}
I have added this js script in this template on the top
<script>
$("#state").on('click', function () {
var username = $(this).val();
var isApproved = true // or false, you have to set it
var id = application.id // you have to set it
$.ajax({
url: '/ajax/change_status/',
data: {
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
'isApproved': isApproved
'id': username
},
dataType: 'json',
success: function (data) {
if (data.success) {
alert("ajax call success.");
// here you update the HTML to change the active to innactive
}else{
alert("ajax call not success.");
}
}
});
});
</script>