2

I get get error invalid literal for int() with base 10: '1,303' and it shows the blow code at fault. The old data works fine, its just for new stuff i add to the database. Any ideas?

    def get(self, request, *args, **kwargs):
    cart = self.get_object()
    item_id = request.GET.get("item")
    delete_item = request.GET.get("delete", False)
    flash_message = ""
    item_added = False
    if item_id:
                    item_instance = get_object_or_404(Variation, id=item_id) ...
        qty = request.GET.get("qty", 1)
        try:
            if int(qty) < 1:
                delete_item = True
        except:
            raise Http404
keno
  • 121
  • 1
  • 2
  • 11
  • Is the comma here being used as the decimal mark? What do you expect to do if there are decimal places? – Sayse May 03 '17 at 11:13
  • Related: [How do I use Python to convert a string to a number if it has commas in it as thousands separators?](http://stackoverflow.com/q/1779288/1324033), but use django forms. – Sayse May 03 '17 at 11:14

1 Answers1

1

This is why you should use django forms instead of pulling data direct off request.GET or request.POST. Django forms provide you with data validation which helps you avoid this kind of thing. The problem is the extra ',' in your supposed int.

int('1,303')

Will produce the error above. The following will not:

int('1,303'.replace(',',''))

So the obvious remedy then is to pass your int through such a filter.

item_instance = get_object_or_404(Variation, id=int(item_id.replace(',',''))
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • In european countries, `,` is used instead of `.` as a decimal point, so this may not achieve the same result – Sayse May 03 '17 at 11:11