由于js中不能使用django template所使用的语言,不能调用django中的变量什么的,所以在使用csrt_token时,可以现在html中加入
<input type="hidden" id="csrf_token" value="{{ csrf_token }}">
再通过js或者jquery去取出这个input的value
$('#like').click(function(){
$.ajax({
type: "POST",
url: "/like",
data: {'id': $(this).attr('name'), 'csrfmiddlewaretoken': $('#csrf_token').val() },
dataType: "text",
success: function(response) {
var d = $.parseJSON(response);
$('#n_likes').text(d.n_likes)
},
error: function(rs, e) {
alert(rs.responseText);
}
});
})