This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

How to handle python djangon geocoder osm error if user enter invalid city name

0

Did anyone know how to handle python geocoder osm error if user enter invalid city name? I am using it in Django. When user input valid city name it saving to my database and showing on map but I can't stop user to put invalid city name and prevent saving to my database. here is part of my code:

def Distance_view(request):
    if request.method == 'POST':
        form = Search(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = Search()


    address = FindDistance.objects.all().last()
    location = geocoder.osm(address)

right now if user give valid address then it showing on map but if user give wrong address it's saving to my database and raise this error. After deleting the wrong address from my database it's start working. How to prevent user to give wrong address and prevent saving to my database.

"ValueError at /map

Location should consist of two numerical values, but None of type <class 'NoneType'> is not convertible to float."

asked 08 Apr '21, 16:07

tusar25's gravatar image

tusar25
11113
accept rate: 0%

edited 08 Apr '21, 16:09


One Answer:

1

You would have to check that your location variable has the right type of data before saving it to a database. As your geocoder probably uses nominatim, it would return an empty result if the city name entered can't be found.

answered 09 Apr '21, 08:35

Spiekerooger's gravatar image

Spiekerooger
3.1k22356
accept rate: 16%

Source code available on GitHub .