Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在不刷新页面Django/Ajax的情况下添加到购物车(内部服务器错误)_Ajax_Django_Forms - Fatal编程技术网

尝试在不刷新页面Django/Ajax的情况下添加到购物车(内部服务器错误)

尝试在不刷新页面Django/Ajax的情况下添加到购物车(内部服务器错误),ajax,django,forms,Ajax,Django,Forms,我有这个错误cart/create/?product\u id=1500(内部服务器错误)我不明白为什么:)我试图先使用Ajax。尝试在购物车中添加产品而不刷新页面 从此链接添加产品 <a href="#" data-id="{{ product.id }}" class="add_to_cart"><button>Add product</button></a> 我的看法 def cart_create(request): cart =

我有这个错误
cart/create/?product\u id=1500(内部服务器错误)
我不明白为什么:)我试图先使用Ajax。尝试在购物车中添加产品而不刷新页面

从此链接添加产品

<a href="#" data-id="{{ product.id }}" class="add_to_cart"><button>Add product</button></a>
我的看法

def cart_create(request):
    cart = Cart(request)
    product_id = request.GET.get('product_id')
    product = Product.objects.get(id=product_id)
    cart.add(product=product)

    return JsonResponse('')
js

有关
JsonResponse
的说明,请参阅


您需要将
dict
传递给
JsonResponse
,而不仅仅是一个空字符串。或者,如果设置了
safe=False
,则可以传递json可序列化对象。但即使这样,我也不认为空字符串是有效的JSON。

向我们展示完整的错误堆栈trace@dirkgroten,更新的
JsonResponse
将dict作为其
数据
参数,而不是字符串。即使在成功的情况下,发送回一些东西也是一种很好的做法,例如
JsonResponse({'status':'OK'})
,但是
JsonResponse({})
也会起作用。@dirkgroten,噢,谢谢你:它帮助了我)
def cart_create(request):
    cart = Cart(request)
    product_id = request.GET.get('product_id')
    product = Product.objects.get(id=product_id)
    cart.add(product=product)

    return JsonResponse('')
<script type="text/javascript">
    $(document).ready(function(){
        $('.add_to_cart').on('click', function(){
            product_id = $(this).attr('data-id')
            data = {
                product_id: product_id
            }

            $.ajax({
                type: 'GET',
                url: '{% url "cart:cart_create" %}',
                data: data,
                success: function(data){
                    console.log('success')
                }
            })
        })
    });
</script>
<header>
    {% with total_items=cart|length %}
        {% if cart|length > 0 %}
            Our cart:

            <a href="{% url 'cart:cart_show' %}" id="cart_count">
                {{ total_items }} products {{ cart.get_total_price }} &#8381;
            </a>
        {% else %}
            <a href="{% url 'cart:cart_show' %}">Cart is empty</a>
        {% endif %}
    {% endwith %}

    <h1><a href="{% url 'shop:product_list' %}">Main</a ></h1>
</header>

terminal
Internal Server Error: /cart/create/
Traceback (most recent call last):
  File "/home/kory/project/django-shop/.env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/kory/project/django-shop/.env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/kory/project/django-shop/.env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/kory/project/django-shop/cart/views.py", line 21, in cart_create
    return JsonResponse('')
  File "/home/kory/project/django-shop/.env/lib/python3.7/site-packages/django/http/response.py", line 552, in __init__
    'In order to allow non-dict objects to be serialized set the '
TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.
console.log
jquery.min.js:4 GET http://localhost:8000/cart/create/?product_id=1 500 (Internal Server Error)