简体   繁体   English

添加到购物车后如何留在同一产品上?

[英]How to stay on Same Product after Add to Cart?

I make my E-Commerce Website using Django.我使用 Django 制作我的电子商务网站。 When I use Add to Cart functionality on the website refresh the page & I go to the top of the page.当我在网站上使用添加到购物车功能时,刷新页面并将 go 到页面顶部。 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. function 是在 Django 中使用 Session 制成的,而不是 JS。

So I want when users use Add To Cart Function user stay on the same product.所以我希望当用户使用添加到购物车 Function 时用户留在同一产品上。

I think this thing is possible using JS.我认为使用 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>

user reverse function用户反向function

and in the function that handles the AddCart it should be something like this in views.py在处理 AddCart 的 function 中,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和 url.py 应该看起来像这样

    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另一种方法是检查请求是 POST 还是 GET,如果它得到了,你应该正常呈现模板页面,如果它是 POST,则使用反向 function

hope that help希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM