我正在建立一个电子商务网站,现在我有一个立即购买按钮,当按下该按钮时,会将商品添加到购物车并重定向到“购物车”页面,我想要一个“ addtocart” botton,它将添加产品到卡片,但在相同功能下不会重定向。请帮助我
carts / modes.py
def buy_now(request, slug):
request.session.set_expiry(120000)
try:
the_id = request.session['cart_id']
except:
new_cart = Cart()
new_cart.save()
request.session['cart_id'] = new_cart.id
the_id = new_cart.id
cart = Cart.objects.get(id=the_id)
try :
product = Product_info.objects.get(slug=slug)
except Product_info.DoesNotExist:
pass
product_var = []
if request.method == 'POST':
qty = request.POST['qty']
for item in request.POST:
key = item
val = request.POST[key]
print(key,val)
try:
v = Variation.objects.get(product = product,cat__iexact=key,title__iexact=val)
product_var.append(v)
except:
pass
cart_item = CartItem.objects.create(cart = cart,product = product)
if len(product_var) > 0:
cart_item.variations.add(*product_var)
cart_item.quantity = qty
cart_item.save()
return HttpResponseRedirect(reverse("cart"))
return HttpResponseRedirect(reverse("cart"))
template.html
<form class="form pull-right" method='POST' action="{% url 'buy_now' product.slug %}"> {% csrf_token %}
{% if product.variation_set.all %}
{% if product.variation_set.models %}
<select class = "form-control" name='model'>
{% for item in product.variation_set.models %}
<option value ="{{ item.title }}">{{ item.title }}</option>
{% endfor %}
</select>
{% endif %}
{% if product.variation_set.sizes %}
<select class = "form-control" name='size'>
{% for item in product.variation_set.sizes %}
<option value ="{{ item.title }}">{{ item.title }}</option>
{% endfor %}
</select>
{% endif %}
{% if product.variation_set.colors %}
<select class = "form-control" name='color'>
{% for item in product.variation_set.colors %}
<option value ="{{ item.title }}">{{ item.title }}</option>
{% endfor %}
</select>
{% endif %}
{% endif %}
<input class = 'form-control' name="qty" type="number"/ value="1"/>
<input class = 'btn btn-danger'type="submit" value="Buynow"/>
</form>
答案 0 :(得分:2)
您可以在同一视图中使用它,并改为使用具有name
属性的按钮。
<input class = 'btn btn-danger' type="submit" name="buynow" value="Buynow"/>
<input class = 'btn btn-danger' type="submit" name="addtocart" value="Add to Cart"/>
然后在您的视图中:
if request.method == 'POST':
if 'buynow' in request.POST:
....
return HttpResponseRedirect(reverse("cart"))
elif 'addtocart' in request.POST:
# code to response add to cart button
答案 1 :(得分:2)
您的问题是不完整的,但是我认为您需要重定向并执行POST
操作(添加到购物车和立即购买)。
您模板中的按钮
<input class = 'btn btn-danger'type="submit" value="Buynow"/>
更新
<input class = 'btn btn-primary' type="button" value="Add to Cart" id="id_AddtoCart/>
<input class = 'btn btn-danger' type="submit" value="Buynow"/>
并为添加到购物车功能
编写AJAX POST
方法
$("#id_AddtoCart").click(function() {
$.ajax({
method: "POST",
url: "/cart/ajax/addtocart/", #example URL, replace with your ajax
data : $('#cartform').serialize(),
success: function(result){
// template update code from the result
}
})
});
添加到购物车的视图应如下图所示 将表单,模型和字段更改为实际值
def add_to_cart(request):
if request.method == 'POST':
form = CartItemForm(request.POST)
if form.is_valid():
cart = CartItem.add(product=form.cleaned_data['product'], quantity=form.cleaned_data['quantity'])
data = {"status": "success", "total": cart.qty, "count": cart.count }
return HttpResponse(json.dumps(data), content_type="application/json")
return HttpResponse(json.dumps({"status": "error"}), content_type="application/json")
然后,您可以通过“立即购买”按钮使用立即购买视图提交表单
最好将AJAX功能用于添加到购物车
之类的功能def buy_now(request, slug):
request.session.set_expiry(120000)
try:
the_id = request.session['cart_id']
except:
new_cart = Cart()
new_cart.save()
request.session['cart_id'] = new_cart.id
the_id = new_cart.id
cart = Cart.objects.get(id=the_id)
try :
product = Product_info.objects.get(slug=slug)
except Product_info.DoesNotExist:
pass
product_var = []
if request.method == 'POST':
qty = request.POST['qty']
for item in request.POST:
key = item
val = request.POST[key]
print(key,val)
try:
v = Variation.objects.get(product = product,cat__iexact=key,title__iexact=val)
product_var.append(v)
except:
pass
cart_item = CartItem.objects.create(cart = cart,product = product)
if len(product_var) > 0:
cart_item.variations.add(*product_var)
cart_item.quantity = qty
cart_item.save()
return HttpResponseRedirect(reverse("cart"))