Vitalii Bashynskyi
Vitalii Bashynskyi

Reputation: 45

how to realise redirect to same page in Django views?

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!

Upvotes: 1

Views: 1978

Answers (3)

Tariq Ahmed
Tariq Ahmed

Reputation: 486

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

Upvotes: 3

yagus
yagus

Reputation: 1445

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.

Upvotes: 1

Rvector
Rvector

Reputation: 2452

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

return redirect('.')

Upvotes: 0

Related Questions