cipher
cipher

Reputation: 393

How to redirect to the same page after an action in django

I have this function add_to_cart once a product is added to cart it should redirect to the same.( since the the answers have not solved the issue ). I have changed at least should redirect to product detail page requires to pass an argument either slug or product id any idea

def add_to_cart(request, **kwargs):
    ------------------------
    ------------------------
    messages.info(request, "item added to cart")
    return redirect(reverse('products:single_product',args=(request)))

url for product detail

url(r'^(?P<slug>.*)/$',single, name="single_product"),

Upvotes: 3

Views: 6227

Answers (5)

Use "request.path" to get the current url as shown below:

# "views.py"

from django.shortcuts import redirect

def my_view(request):
    return redirect(request.path) # Here

Or, use "request.path_info" to get the current url as shown below:

# "views.py"

from django.shortcuts import redirect

def my_view(request):
    return redirect(request.path_info) # Here

Upvotes: 0

Higor Rossato
Higor Rossato

Reputation: 2046

You can do:

return HttpResponseRedirect(request.path_info)

You can also read about how Request/Response works in Django but also about the request.path_info in the docs

Upvotes: 4

Lag11
Lag11

Reputation: 542

You can use request.path to find the current url of the request and then redirect.

from django.shortcuts import redirect

return redirect(request.path)

Upvotes: 3

Mehrdad
Mehrdad

Reputation: 131

HttpRequest

from django.http import HttpResponseRedirect


return HttpResponseRedirect(request.path_info)

Upvotes: 0

a-c-sreedhar-reddy
a-c-sreedhar-reddy

Reputation: 578

Send the address where you are calling from in kwargs and redirect to that.

Upvotes: 0

Related Questions