TypeError: аргумент int() должен быть строкой, байтоподобным объектом или действительным числом, а не 'NoneType'

я делаю сайт электронной коммерции на django по этому учебнику youtube ecommerce tutorial.Я создал функцию для удаления из корзины, но когда я нажимаю кнопку удаления, я получаю это сообщение

TypeError at /cart/delete/ int() Аргумент должен быть строкой, байтоподобным объектом или действительным числом, а не 'NoneType'>. байтоподобный объект или вещественное число, а не 'NoneType'

здесь функция

def cart_delete(request):
    cart = Cart(request)
    if request.POST.get('action') == 'post':
        product_id = int(request.POST.get('productid'))
        cart.delete(product=product_id)
        response = JsonResponse({'success':True})
        return response

> TypeError at /cart/delete/ int() argument must be a string, a
> bytes-like object or a real number, not 'NoneType' Request
> Method:   POST Request URL:   http://127.0.0.1:8000/cart/delete/ Django
> Version:  3.2.8 Exception Type:   TypeError Exception Value:   int()
> argument must be a string, a bytes-like object or a real number, not
> 'NoneType' Exception
> Location: C:\Users\DELL\Desktop\ecommerce\cart\views.py, line 30, in
> cart_delete Python
> Executable:   C:\Users\DELL\Desktop\ecommerce\venv\scripts\python.exe
> Python Version:   3.10.0

удалить функцию

def delete(self, product):
        product_id = str(product) 
         
        if product_id in self.cart:
            del self.cart[product_id]
            
        self.session.modified = True 

мой ajax скрипт

<script>
        $(document).on('click', '#delete-button', function(e){
            e.preventDefault();
            console.log($('#select option:selected').text())
            $.ajax({
                type:'POST',
                url:'{% url "cart:cart_delete" %}',
                data:{
                    productid: $('#add-button').val(),
                    csrfmiddlewaretoken:"{{csrf_token}}",
                    productqty: $('#select option:selected').text(),
                    action: 'post'
                },
                success: function (json){
                     
                },
                error: function (xhr, errmsg, err){}
            })
        })
        
      
    </script>

Помогите пожалуйста.

Ваши productid в request.POST пустые. Проверьте в консоли браузера значение с помощью $('#add-button').val().

Проверить, существует ли он, можно с помощью оператора in.


if productid in request.POST:
    product_id = int(request.POST.get('productid'))
    cart.delete(product=product_id)
    response = JsonResponse({'success':True})
else :
    response = JsonResponse({'success': False})


Вернуться на верх