简体   繁体   English

Django添加到购物车按钮返回ERR_EMPTY_RESPONSE错误

[英]Django Add to Cart Button Returns ERR_EMPTY_RESPONSE error

I have an add to cart button in my Django Application. 我的Django应用程序中有一个添加到购物车按钮。 When i click to add to cart button i get a ERR_EMPTY_RESPONSE error but the product is added to cart. 当我单击添加到购物车按钮时,出现ERR_EMPTY_RESPONSE错误,但产品已添加到购物车。 I am able to see my cart when i click to cart link that placed right top on my page but i can not see it when i click to add to cart button. 当我单击放在页面右上角的购物车链接时,我能够看到我的购物车,但是当我单击添加到购物车按钮时,我看不到它。 It goes to nothing. 它什么都没有。 What might cause this problem ? 什么可能导致此问题? any help would be appriciated. 任何帮助都会得到应用。

def add_to_cart(request):
    postdata = request.POST.copy()
    # get product slug from post data, return blank if empty
    product_slug = postdata.get('product_slug','')
    # get quantity added, return 1 if empty
    quantity = postdata.get('quantity',1)
    # fetch the product or return a missing page error
    p = get_object_or_404(Product, slug=product_slug) #django shortcuts helper method
    #get products in cart
    cart_products = get_cart_items(request)
    product_in_cart = False
    # check to see if item is already in cart
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            # update the quantity if found
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        # create and save a new cart item
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()

You should return a resonse. 您应该返回一个共鸣。 for example like this: 例如这样的:

import render_to_response by: 通过以下方式导入render_to_response

from django.shortcuts import render_to_response

And use it in your view: 并在您的视图中使用它:

return render_to_response( 'your_template')

also take a look at this . 也来看看这个

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

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