0

I make my E-Commerce Website using Django. When I use Add to Cart functionality on the website refresh the page & I go to the top of the page. But I want the user to stay on the same Product after Add to Cart.

Add To Cart function is made using Session in Django, not JS.

So I want when users use Add To Cart Function user stay on the same product.

I think this thing is possible using JS.

This is Add to Cart button

<form action="{% url 'cart:AddCart' %}" method="POST">{% csrf_token %}
      <input hidden type="text" name="product" value="{{i.id}}">
      <button id="clickMe" class="main-btn cart cart-btn" style="padding: 5px 32px">Add <i class="fa-solid fa-cart-shopping"></i></button>
</form>

1 Answers1

0

user reverse function

and in the function that handles the AddCart it should be something like this in views.py

def addcart(request,id):
 if request.method == "POST":
    //handle the request and store it to the database
    return HttpResponseRedirect(reverse("addcart", args=(id,)))

and the url.py should look something like this

    path("addtocat/<int:id>", views.listing, name="addcart"),

other way is checking if the request is POST or GET , if it's get you should render the template page normally if it's POST use the reverse function

hope that help