我正在尝试使用带Django的ajax删除对象。我发送请求方法'POST'下注'GET'并提出错误
function delete_bank_question(pk){
if (confirm('are you sure you want to remove this question?')==true){
url = "/question/delete/"
console.log("Deleted clicked");
$.ajax({
url : url,
type : "POST",
method : "post",
data : {'pk': pk},
success : function(json) {
$('#list_question_pk_'+pk).hide();
console.log("element deletion successful");
$("#deleteModal").toggle();
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText);
console.log("error when removing element");
}});
}
else {
return false;
}
};
并且有视图
class QuestionDeleteView(DeleteView):
model = Question
template_name = 'questionbank.html'
def get_object(self, queryset=None):
pk = self.request.POST.get("pk")
element = Question.objects.get(pk=pk)
return element
def get_success_url(self):
return reverse('questions:questionbank')
唯一的区别是js用ajax(请求GET)
绘制HTMLfunction item_builder(data,pk){
var item = ['<li class="questions-item text-center" id="list_question_pk_' +pk+ '">'];
item.push('<div class="col-md-4 "><a href="#" class="question-name pull-right">');
item.push(data.description);
item.push('</a></div>');
item.push('<div class="col-md-4">');
item.push('<p class="question-type '+css_mapper[data.question_type]+'">');
item.push(type_mapper[data.question_type]);
item.push('</p>');
item.push('</div>');
item.push('<div class="col-md-4">');
item.push('<p class="pull-left">');
item.push('<i class="fa fa-ellipsis-h"></i>');
item.push('<div class="pull-left question-setting" >');
item.push('<a href="#" id="edit-icon" class="wow fadeInDown"');
item.push('data-wow-duration="0.5s">');
item.push('<i class="fa fa-pencil fa-1x"></i>');
item.push('</a>')
item.push('<a href="#" data-toggle="modal" data-pk="'+pk+'" id="delete-icon" ');
item.push('data-target="#deleteModal" class="wow fadeInDown delete-icon"');
item.push('data-wow-duration="0.3s">');
item.push('<i class="fa fa-trash-o fa-1x"></i>');
item.push('</a>')
item.push('</div>');
item.push('</p>');
item.push('</div>');
item.push('</li>');
return item.join(' ');
}
并且有来自inspect元素
的结果PK: 1
list_questions.js:105 Deleted clicked
jquery-1.11.2.min.js:4 GET http://127.0.0.1:8000/en/question/delete/
500 (INTERNAL SERVER ERROR)m.ajaxTransport.a.send @ jquery-
1.11.2.min.js:4m.extend.ajax @ jquery-
1.11.2.min.js:4delete_bank_question @ list_questions.js:106(anonymous
function) @ list_questions.js:40m.event.dispatch @ jquery-
1.11.2.min.js:3m.event.add.r.handle @ jquery-1.11.2.min.js:3
list_questions.js:118 500: DoesNotExist at /en/question/delete/
Question matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/en/question/delete/
谁能帮帮我?
答案 0 :(得分:0)
您的检查元素结果显示您正在通过GET方法发送请求:
...
jquery-1.11.2.min.js:4 GET http://127.0.0.1:8000/en/question/delete/
...
首先检查您的JS代码。
答案 1 :(得分:0)