I get quite some traffic for the term “invalid literal for int with base 10″. I guess a lot people fumbling around with Python or Django are getting this error message once in a while. This happens when you try to cast a string into an integer and this string does not really contain a “digit”:

Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> string = “TestString”
>>>number = int(string)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ValueError: invalid literal for int() with base 10: ‘TestString’
>>>string = “1″
>>>number = int(string)
>>>number
1

If you work with the Django framework and get this error while trying to request a web page I would check the parameters. Since every parameter that is getting passed in a request is a string you probably cast them into integers somewhere in your source code. It can be  a category id or similar. Now when that category id for whatever reason is empty or does not contain a string that can be cast into an integer, you will get the “invalid literal for int with base 10″ error message.

To avoid this error you should check if the string contains a digit:

Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> string = “1″
>>> if string.isdigit():
…         number = int(string)
…     else:
…         print “The variable String contains no digit”

>>> number
1

str.isdigit()

Return true if all characters in the string are digits and there is at least one character, false otherwise.
For 8-bit strings, this method is locale-dependent.

More on String operations can be found in the Python documentation.