Redirecting after AJAX post in Django

前端 未结 3 1686
灰色年华
灰色年华 2020-12-08 02:51

I use Django\'s built-in DeleteView and I\'ve assigned a value to the success_url attribute. Now in my template, I trigger this view via JQuery\' $.post() metho

3条回答
  •  醉酒成梦
    2020-12-08 03:46

    http://hunterford.me/how-to-handle-http-redirects-with--jquery-and-django/

    Edit: source unavailable

    this one works for me, perhaps looks like hack

    django middleware

    from django.http import HttpResponseRedirect
    
    class AjaxRedirect(object):
        def process_response(self, request, response):
            if request.is_ajax():
                if type(response) == HttpResponseRedirect:
                    response.status_code = 278
            return response
    

    javascript

    $(document).ready(function() {
        $(document).ajaxComplete(function(e, xhr, settings) {
            if (xhr.status == 278) {
                window.location.href = xhr.getResponseHeader("Location");
            }
        });
    });
    

提交回复
热议问题