У меня есть эта ошибка cart/create/?product_id=1 500 (Internal Server Error)
Я не понимаю почему :) Сначала я пытаюсь использовать Ajax.Попробуйте добавить товар в корзину без обновления страницы.
Добавить товар по этой ссылке
<a href="#" data-id="{{ product.id }}" class="add_to_cart"><button>Add product</button></a>
мой URL
url(r'^cart/create/$', views.cart_create, name='cart_create'),
мой взгляд
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
<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>
Этот файл base.html, здесь я пытаюсь вывести мой счет в корзину, и это работает, но только с обновлением
<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 }} ₽
</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)