dor272
dor272

Reputation: 738

django redirect after delete

After deleting an object from my DB I want to redirect to a certain view.

here is my view where the deletion happens:

def client_delete(request):
    if request.method == 'GET':
        return _not_exist_page(request)
    else:
        client = Client.objects.get(id=request.POST['id'])
        client.delete()
        print('deleted')
        return redirect('clients:index')
        print('deleted2')

when I delete an object here's what I see on the terminal:

deleted
[03/Apr/2018 15:55:50] "POST /clients/delete/ HTTP/1.1" 302 0
[03/Apr/2018 15:55:50] "GET /clients/ HTTP/1.1" 200 7467

it means that the redirect is triggered(and that's why the second print doesn't show up) but view in my browser doesn't change.

Any idea why that happens?

Thanks

Upvotes: 0

Views: 3822

Answers (2)

Raja Simon
Raja Simon

Reputation: 10315

You are making the jQuery POST request to server which means server can't control what to show next. My suggestion is to give the JsonResponse back to server like {'status': True, etc...} and change the view based on your server response. You can use javascript window to change the view.

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

Upvotes: 3

Gregory
Gregory

Reputation: 7242

You can do:

from django.shortcuts import redirect

return redirect('person_list')

Where person_list is your URL name

url('delete/<int:id>/', persons_delete, name="persons_delete")

Upvotes: 0

Related Questions