Home > Software engineering >  The view products.views.get_product didn't return an HttpResponse object. It returned None inst
The view products.views.get_product didn't return an HttpResponse object. It returned None inst

Time:10-28

Here is my code, I am getting the same error again and again.

def add_to_cart(request,uid):
    variant=request.GET.get('variant')
    product=Product.objects.get(uid=uid)
    user=request.user
    cart=Cart.objects.get_or_create(user=user, is_paid=False)
    cart_item=CartItems.objects.create(cart=cart,product=product)

    if variant:
        variant=request.GET.get('variant')
        size_variant=SizeVariant.objects.get(size_name=variant)
        cart_item.size_variant=size_variant
        cart_item.save()
        return HttpResponseRedirect(request.META.get('HTTP_REFFER'))

ValueError at /product/tommy-hilfiger-blue-jeans The view products.views.get_product didn't return an HttpResponse object. It returned None instead. Request Method: GET

Django Version: 4.0.4 Exception Type: ValueError Exception Value:
The view products.views.get_product didn't return an HttpResponse object. It returned None instead. Exception Location: C:\Users\MyPc\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py, line 332, in check_response Python Executable: C:\Users\MyPc\AppData\Local\Programs\Python\Python39\python.exe Python Version: 3.9.7

CodePudding user response:

In your code, if the if variant: condition fails, then there is no HttpResponse, means it return None.

So try printing the variable variant and see whats wrong.

CodePudding user response:

Just add return HttpResponse outside if

def add_to_cart(request,uid):
    variant=request.GET.get('variant')
    product=Product.objects.get(uid=uid)
    user=request.user
    cart=Cart.objects.get_or_create(user=user, is_paid=False)
    cart_item=CartItems.objects.create(cart=cart,product=product)

    if variant:
        variant=request.GET.get('variant')
        size_variant=SizeVariant.objects.get(size_name=variant)
        cart_item.size_variant=size_variant
        cart_item.save()
        return HttpResponse(request.META.get('HTTP_REFFER'))

    return HttpResponse(0)
  • Related