Django form geocode submitted address to get lat, lon and postal code

One of my Django applications includes a form where user can enter and submit a property address.

django form

The user submitting the form might not know the postal code so I left it optional. However the postal code is a key piece of information for this particular application so I wanted to ensure that I was getting it.

I also wanted to geocode the address immediately to get the address latitude and longitude so it could be shown to user on a Leaflet.js map.

There are lots of free geocoding services for low intensity usage but I ended up using Google Geocoding which is free under certain usage level. You just need to create a Geocoding API project and use the credentials to set up the geocoding.

To interact with the geocoding API I tried Python gecoding modules geopy and geocoder but in the end just used Python Requests module instead as it was less complicated.

When the user clicked the Submit button, behind the scenes, Requests submitted the address to Google’s Geocoding API, gets the JSON response containing the latitude, longitude and postal code which are then written to the application database.

I will update the code in future to check if the user’s postal code is correct and replace it if it is incorrect. Will wait to see how the postal code accuracy looks. Making geocoding API requests too often could bump me over the free usage limit.

The Django View that contains the code described above is shown below.

def property_add(request):
    property_list = Property.objects.filter('created')
    if request.method == 'POST':
        form = PropertyForm(request.POST)
        if form.is_valid():
            new_property =
            address = "%s, %s, %s, %s" % (new_property.address1,, new_property.state, new_property.postcode)
            google_geocode_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            url = '' + "'" + address + "'" + '&key=' + google_geocode_key
                response = requests.get(url)
                geoArray = response.json()
       = geoArray['results'][0]['geometry']['location']['lat']
                new_property.lon = geoArray['results'][0]['geometry']['location']['lng']
                new_postcode = geoArray['results'][0]['address_components'][7]['long_name']
                new_fsa = geoArray['results'][0]['address_components'][7]['short_name'][:3]
       = None
                new_property.lon = None
                new_postcode = None
                new_fsa = None
            if new_property.postcode:
                new_property.fsa = new_property.postcode[:3]
                new_property.postcode = new_postcode
                new_property.fsa = new_fsa
            new_property.user_id =
            new_property =
            return HttpResponseRedirect(reverse(property, args=(,)))
        form = PropertyForm()

    context_dict = {
        'form': form, 
        'property_list': property_list,
    return render(
        context_instance = RequestContext(
                'title':'Add Property',

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation