NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

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


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.

permanent link

answered 09 Apr '21, 08:35

Spiekerooger's gravatar image

Spiekerooger
3.1k22356
accept rate: 16%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×710
×133
×85
×2

question asked: 08 Apr '21, 16:07

question was seen: 1,616 times

last updated: 09 Apr '21, 08:35

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum