Kiran Baktha
Kiran Baktha

Reputation: 667

Django redirect after ajax on the server

What is the best way to redirect on the server after an ajax request in django. Say I have this view function that receives an ajax call

def view(request):
    if request.is_ajax():
        do_something() # This function does something on the server
        return redirect('') # Redirect back to the same view's get

It seems like the server redirects but on the client side the browser is not refreshed because the post method was through Ajax. I have some ideas in mind but they may not be so optimal. Is there an optimal way to go about this?

Thanks.

Upvotes: 0

Views: 1192

Answers (1)

Danil
Danil

Reputation: 5181

Seems weird and impossible to do. Actually, you will get a response from the redirect. You will see it if you open the browser dev tools (network tab). But the current page will be the same. You have to handle redirect on the client side. Here's an example

window.location.href = data.redirect

Upvotes: 2

Related Questions