invalid literal for int() with base 10: ''

51 views
Skip to first unread message

Lutalo Bbosa joseph

unread,
Jun 21, 2019, 8:33:50 AM6/21/19
to django...@googlegroups.com
hi guys, am working on an ecommerce system, which has cart as an app, but i keep on getting this error and cant move on any help, here is my views.py ,
from the commandline the error is on line 42
views.py.png

anchal agarwal

unread,
Jun 21, 2019, 9:29:52 AM6/21/19
to django...@googlegroups.com

Hello lutalo
I am also facing the same issue. If you find any solution please let me know . It would be very helpful.
Thank you

hi guys, am working on an ecommerce system, which has cart as an app, but i keep on getting this error and cant move on any help, here is my views.py ,
from the commandline the error is on line 42

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMz%3Dh%3DTQikVLrVTnwQse3r5P-rxCvaHFRP-qnYWrYSWGHPSGgw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ahmed Ishtiaque

unread,
Jun 21, 2019, 11:20:57 AM6/21/19
to django...@googlegroups.com
Hi Lutalo,

Could you also share the stacktrace of the error and when it happens? It would help us decipher what's really going on in relation to what you're trying to do. 

Best,
Ahmed

On Fri, Jun 21, 2019 at 8:33 AM Lutalo Bbosa joseph <bbo...@gmail.com> wrote:
hi guys, am working on an ecommerce system, which has cart as an app, but i keep on getting this error and cant move on any help, here is my views.py ,
from the commandline the error is on line 42

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Lutalo Bbosa joseph

unread,
Jun 21, 2019, 11:35:38 AM6/21/19
to django...@googlegroups.com
well here is apic of what am doing,

remove.png

Lutalo Bbosa joseph

unread,
Jun 21, 2019, 11:36:23 AM6/21/19
to django...@googlegroups.com
on pressing remove the product is supposed to be removed from the cart, but it instead displays an error

Lutalo Bbosa joseph

unread,
Jun 21, 2019, 11:42:21 AM6/21/19
to django...@googlegroups.com
i as well have a models.py file for carts, and products which is attached, when i try to search for a solution on stack overflow,all solutions insist on problem with integers and strings in my code. though these were passed solutions and not too similar to this one
cart_models.png

Aldian Fazrihady

unread,
Jun 21, 2019, 11:59:27 AM6/21/19
to django...@googlegroups.com
Does the stack trace mention a template variable name?  It looks like a template variable that is expected to have integer value is not properly initialized.

Regards, 

Aldian Fazrihady

Lutalo Bbosa joseph

unread,
Jun 21, 2019, 12:13:26 PM6/21/19
to django...@googlegroups.com
here is my template that handles that bit, and i have as well attached atemplate in which its used

update_cart.png
template.png

Jorge Gimeno

unread,
Jun 21, 2019, 12:46:03 PM6/21/19
to django...@googlegroups.com
Would you be able to copy and paste the stack trace here? Without that, it's really hard to see where this exception is coming from.

-Jorge

Lutalo Bbosa joseph

unread,
Jun 21, 2019, 1:47:59 PM6/21/19
to django...@googlegroups.com

ValueError at /carts/update/

invalid literal for int() with base 10: '' thats the error


Héctor Alonso Lozada Echezuría

unread,
Jun 21, 2019, 2:56:23 PM6/21/19
to django...@googlegroups.com
Apparently product_id has no value

product_id = request.POST.get("product_id")
print(product_id)
print(type(product_id))


El vie., 21 jun. 2019 a las 6:33, Lutalo Bbosa joseph (<bbo...@gmail.com>) escribió:
hi guys, am working on an ecommerce system, which has cart as an app, but i keep on getting this error and cant move on any help, here is my views.py ,
from the commandline the error is on line 42

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMz%3Dh%3DTQikVLrVTnwQse3r5P-rxCvaHFRP-qnYWrYSWGHPSGgw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Héctor Alonso Lozada Echezuría

Lutalo Bbosa joseph

unread,
Jun 21, 2019, 3:17:36 PM6/21/19
to django...@googlegroups.com
hi hector, can u help me fix it, coz am kinda puzzled




Lutalo Bbosa joseph

unread,
Jun 21, 2019, 3:34:29 PM6/21/19
to django...@googlegroups.com
am trying to figure it out but when i
print(product_id)
print(type(product_id)) i get this output
1
<class 'str'>, so probably its because i cant iterate over a string but am still failing to figure it out

Héctor Alonso Lozada Echezuría

unread,
Jun 21, 2019, 3:50:11 PM6/21/19
to django...@googlegroups.com
Of course, 

Share your source code and stack trace through pastebin




For more options, visit https://groups.google.com/d/optout.

Amiya Dash

unread,
Jun 22, 2019, 12:29:53 PM6/22/19
to Django users
Can u send me the full project.share me the github link

Alejandro Pena

unread,
Jun 22, 2019, 12:50:42 PM6/22/19
to Django users
I feel you, figuring out such errors for the first time can be infinitely frustrating.

I believe that the form is sending you product id numbers as strings, or 1 as ‘1’.

If the product_id = ‘1’ then product_id is not None.

However, you have defined product_id in the model for Product as an int and it’s raising a TypeError because you’re searching for a product_id that is a string instead.

To fix the problem, convert the result from the POST request from a string into an int.

After you check if product_id is not None:
product_id = int(product_id)

Regards,
Alejandro Peña

Lutalo Bbosa joseph

unread,
Jun 22, 2019, 12:51:18 PM6/22/19
to django...@googlegroups.com
kk will do that,

On Sat, Jun 22, 2019 at 7:29 PM Amiya Dash <amiya.ran...@gmail.com> wrote:
Can u send me the full project.share me the github link

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Lutalo Bbosa joseph

unread,
Jun 22, 2019, 12:59:34 PM6/22/19
to django...@googlegroups.com
This is what happens in the terminal when i use int(product_id)
2
<class 'str'>
2
[22/Jun/2019 16:56:33] "POST /carts/update/ HTTP/1.1" 302 0
[22/Jun/2019 16:56:33] "GET /carts/ HTTP/1.1" 200 8245



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Alejandro Pena

unread,
Jun 22, 2019, 1:17:34 PM6/22/19
to Django users
Sweet, looks like your code is working!

You’re still seeing the ‘2’ and that the type of product_id is a string because your print statements are placed before you convert product_id into an int and then use it to search for product_obj.

The evidence that it’s working is that it didn’t raise an error this time and then you see the POST and GET requests successfully completed with response codes 302 (return redirect to /carts) and 200 (return render...).

Lutalo Bbosa joseph

unread,
Jun 22, 2019, 1:33:02 PM6/22/19
to django...@googlegroups.com
but the remove function is supposed to work yet it returns the error but add works
def update(request):

product_id = request.POST.get("product_id")
    if product_id is not None :
        print(product_id)
        product_id = int(product_id)
       
        try:
            product_id = int(product_id)
            product_obj = Product.objects.get(id=product_id)
            #product_obj = Product.objects.get(id=product_id)
        except Product.DoesNotExist:
            print("message to user, product is gone")
            return redirect("carts:home")
        cart_obj, new_obj = Cart.objects.new_or_get(request)
        if product_obj in cart_obj.products.all():
            cart_obj.products.remove(product_obj)
            added = False

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Alejandro Pena

unread,
Jun 22, 2019, 1:59:55 PM6/22/19
to Django users
Which error are you seeing now? Try making this change and let us know what the results are.

cart_obj, new_obj = Cart.objects.get(user=request.user)


if product_obj in cart_obj.products.all():
cart_obj.products.remove(product_obj)
added = False

Context: http://www.learningaboutelectronics.com/Articles/How-to-check-if-an-object-is-in-a-ManyToManyField-in-Django.php

Lutalo Bbosa joseph

unread,
Jun 22, 2019, 3:12:00 PM6/22/19
to django...@googlegroups.com
it all is not working, i guess i have to write another independent function for reomving products from the cart
but thx. though i thought, it would workout

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Alejandro Pena

unread,
Jun 22, 2019, 5:18:47 PM6/22/19
to Django users
Try this instead:

cart_obj, new_obj = Cart.objects.get_or_create(user=request.user)

You originally had:

cart_obj, new_obj = Cart.objects.new_or_get(request)

There is no .new_or_get method in the Django QuerySet API: https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

Also, inside of the parentheses you must specify a field in the Cart model and a value to search for in order to identify a specific cart: (user = request.user)


Reply all
Reply to author
Forward
0 new messages