Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:01:11+00:00 2026-06-11T16:01:11+00:00

In Django, I have been trying to get a search field to geocode a

  • 0

In Django, I have been trying to get a search field to geocode a location and spit out a list from my db sorted by distance. So far everything works except when I search for a location that Google returns multiple results form such as “ann arbor, MI”. I get the ValueError “Didn’t find exactly one placemark! (Found 2.)” Here is my views.py

from django.shortcuts import render_to_response
from models import CampSite
from geopy import geocoders
from django.contrib.gis.geos import *
from django.contrib.gis.measure import D
from campsites.forms import SearchForm
from django.http import HttpResponseRedirect


def results(request):
    query = request.GET['q']
    g = geocoders.Google(resource='maps')
    location, (lat, lon) = g.geocode(query) 
    pnt = fromstr("POINT(%s %s)" % (lon, lat))
    distance_from_point = {'mi':'2000'}
    results = CampSite.objects.filter(lonlat__distance_lte=(pnt,D(**distance_from_point))).distance(pnt).order_by('distance')
    return render_to_response('results.html',{'location': location, 'lat': lat, 'lon': lon, 'results':results})

The common solution I found online was to change

location, (lat, lon) = g.geocode(query)


to


location, (lat, lon) = g.geocode(query, exactly_one=False)

However, this produced the new ValueError “String or unicode input unrecognized as WKT EWKT, and HEXEWKB.”

This is my first django project I’m doing outside of tutorials, so thankyou for being gentile.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:01:12+00:00Added an answer on June 11, 2026 at 4:01 pm

    In Python, the interpreter is your best friend.

    >>> g.geocode('ann arbor, MI', exactly_one=False)
    [(u'Ann Arbor, MI, USA', (42.2808256, -83.743037799999996)),
     (u'Ann Arbor, MI, USA', (42.307649300000001, -83.8473015))]
    

    If you try your code snippet, you will realize that when used with exactly_one=False, the method returns a list of tuples, not a single tuple – so your code need to be refactored accordingly.

    results = []
    geocodes = g.geocode(query, exactly_one=False)
    for geocode in geocodes:
        location, (lat, lon) = geocode
        pnt = fromstr("POINT(%s %s)" % (lon, lat))
        distance_from_point = {'mi':'2000'}
        results.append(
            CampSite.objects.filter(
                 lonlat__distance_lte=(
                      pnt,
                      D(**distance_from_point)
                 )
            ).distance(pnt).order_by('distance')
        )
    

    The above is untested, but the after the loop you should end up with a list of results. Then you should decide what to do:

    • show just the first (geocodes[0], results[0])
    • ask the user to choose from the list
    • show all results (iterate over zip(geocodes, results) in the template)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get xapian working django haystack for a project im
I have been trying to figure out how to consistently get an absolute path
I am using django-voting package and have been trying to get it's manager get_top()
I am brand new to Python, and have been trying to get the Django
I'm new to Django (and Python) and I have been trying to work out
I have been trying to get a response from a server sending a GET
We installed the django toolbar yesterday on our remote server and have been trying
I'm trying to get started with Django, and have previously worked with CakePHP, and
I have been trying to figure out what is going wrong when I try
Previously I have been trying to host multiple DEMO django projects using a virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.