1

I am writing an online store in Django. How do I redirect to the same page? This is needed to remove an item from the popup cart. The delete from cart function:

def cart_remove_retail(request, slug):
    cart = Cart(request)
    product = get_object_or_404(Product, slug=slug)
    cart.remove(product)
    return  #???

when i try:

return HttpResponseRedirect(request.path_info)

I get round-robin query.

Thanks!

  • I don't think you need to redirect, but you need to refresh data from db. Try to add this: `request.session.flush()` befor return – Christophe Dec 02 '21 at 13:20

3 Answers3

3
from django.http import HttpResponseRedirect

def cart_remove_retail(request, slug):
    cart = Cart(request)
    product = get_object_or_404(Product, slug=slug)
    cart.remove(product)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

Taken from : Redirect to same page after POST method using class based views

Tariq Ahmed
  • 471
  • 3
  • 14
1

Assuming you want to redirect to the page where the request to cart_remove_detail is originating, you can use

return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

Alternatively add a next parameter to the request to cart_remove_detail.

yagus
  • 1,417
  • 6
  • 14
0

To redirect to the same page in django view you can use :

return redirect('.')
Rvector
  • 2,312
  • 1
  • 8
  • 17