Django: Geocoding an address on form submission?

Posted by User on Stack Overflow See other posts from Stack Overflow or by User
Published on 2010-05-02T21:16:56Z Indexed on 2010/05/02 21:48 UTC
Read the original article Hit count: 274

Filed under:
|
|
|

Trying to wrap my head around django forms and the django way of doing things. I want to create a basic web form that allows a user to input an address and have that address geocoded and saved to a database.

I created a Location model:

class Location(models.Model):
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100, null=True)
    postal_code = models.CharField(max_length=100, null=True)
    country = models.CharField(max_length=100)
    latitude = models.DecimalField(max_digits=18, decimal_places=10, null=True)
    longitude = models.DecimalField(max_digits=18, decimal_places=10, null=True)

And defined a form:

class LocationForm(forms.ModelForm):
    class Meta:
        model = models.Location
        exclude = ('latitude','longitude')

In my view I'm using form.save() to save the form. This works and saves an address to the database.

I created a module to geocode an address. I'm not sure what the django way of doing things is, but I guess in my view, before I save the form, I need to geocode the address and set the lat and long. How do I set the latitude and longitude before saving?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-forms