[Fixed]-TypeError: float() argument must be a string or a number in Django distance

8👍

You are defaulting to a None value when the kmdistance is not found in the request.GET directory here

kmdistance = request.GET.get("kmtocity", None)

As such, when you convert None to a float later, it throws up an error

TypeError: float() argument must be a string or a number

To overcome this, wherever you are converting to float, just check that the kmdistance value exists, and then convert it to float

if kmdistance is not None:
    kmdistance = float(kmdistance)

Alternatively, use a different default value, like 0 instead of None (though 0 kmdistance may imply wrong value for km distance, so you can use another default value like 100 which works for you)

Leave a comment