姜戈;确认窗口后如何重定向



我正在研究Django项目。我正在创建一个删除函数,我想知道如何重定向到 ajax 页面。

我现在views.py是这样的。

def delete_post(request, pk):
if request.method == 'DELETE':
post = get_object_or_404(Post, id=pk)
post.delete()
#redirect to a page

js

function deletePost(post) {
if (confirm("Are you sure?")) {
var $post = $(post)
var id = $post.data('id')
$.ajax({
url:'/posts/delete/' + id,
method: 'DELETE',
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRFToken', csrf_token)
}
})
}

}

似乎HttpResponseRedirect不适用于 ajax。 有人可以给我提示吗?或者如果可能的话,我想知道在没有 ajax 的情况下做到这一点的方法。

将 ajax 调用更改为:

$.ajax({
url:'/posts/delete/' + id,
method: 'DELETE',
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRFToken', csrf_token)
},
success: function(result){
location.href = "http://www.example.com/ThankYou.html"
}
})

最新更新