我的 hostelApplication 模型中有一个 booleanField,我想使用 html 按钮更改该字段,但我对 js 了解不多。请任何人告诉我我做错了什么。这是我的models.py,我只展示了必要的字段。
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
我在 urls 中添加了我的 url 路径
url(r'^ajax/change_status/$', views.ajax_change_status, name='ajax_change_status')
这是我的 html 文件
{% 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 %}
我在顶部的这个模板中添加了这个 js 脚本
<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>