0

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

dor272
  • 550
  • 1
  • 6
  • 26

2 Answers2

3

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");
Raja Simon
  • 10,126
  • 5
  • 43
  • 74
0

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")
Gregory
  • 6,514
  • 4
  • 28
  • 26
  • There is no problem triggering view for deleting, Whats not working is the redirection to the index after the object is deleted. what you suggested is exactly what im doing to redirect to the index. – dor272 Apr 03 '18 at 13:17
  • Ok I see. So, 'clients:index' are the appname and view name? – Gregory Apr 03 '18 at 13:20