Skip to main content

Django widget and field for Google Places Autocomplete (address autofill)

Project description

django-google-places-autocomplete

Django widget and form field for Google Places Autocomplete, so users can pick an address from Google’s suggestions instead of typing it manually.

Requirements

  • Django >= 2.2
  • Python >= 3.7
  • A Google Places API (or Maps JavaScript API with Places library) key

Installation

pip install django-google-places-autocomplete

Or install in editable mode from source:

pip install -e path/to/django-google-places-autocomplete

Settings

  1. Add the package to INSTALLED_APPS so static files are found:
INSTALLED_APPS = [
    # ...
    'django_google_places_autocomplete',
]
  1. Set your Google Places API key:
GOOGLE_PLACES_API_KEY = 'your-api-key-here'

Restrict the key to your domain and to the Maps JavaScript API / Places API in Google Cloud Console.

Usage

As a widget (ModelForm)

from django import forms
from django_google_places_autocomplete import GooglePlacesAutocompleteWidget
from .models import MyModel

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = '__all__'
        widgets = {
            'address': GooglePlacesAutocompleteWidget(),
            'business_address': GooglePlacesAutocompleteWidget(country_restriction='ca'),
        }

As a field

from django import forms
from django_google_places_autocomplete import GooglePlacesAddressField

class MyForm(forms.Form):
    address = GooglePlacesAddressField(required=False)
    # Restrict to Canada (default is 'ca')
    us_address = GooglePlacesAddressField(country_restriction='us')

In templates

Include the form’s media so the Google script and the widget script load:

{{ form.media }}
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>

Options

  • country_restriction: Limit suggestions to one country (e.g. 'ca', 'us'). Default is 'ca'. Use None for no restriction.

Publishing to PyPI

When ready:

cd path/to/django-google-places-autocomplete
python -m build
twine upload dist/*

License

BSD.

Postal code and formatted address

By default the widget builds the address from Google address_components, so the postal code is included. The country line is omitted unless you pass include_country_in_address=True.

GooglePlacesAutocompleteWidget(
    include_country_in_address=False,  # default; no trailing "Canada"
    format_address_from_components=True,  # default; includes postal_code
)

To use Google’s formatted_address instead (postal code may be missing in some locales):

GooglePlacesAutocompleteWidget(format_address_from_components=False)

Requiring a suggestion (not free typing)

  1. Use the widget with require_place_selection=True.
  2. Add a companion hidden field per address field: {field_name}_place_id.
  3. Render the hidden input in your template (or loop hidden_fields).
  4. Call require_places_selection(form, 'address_field', ...) in clean().
from django import forms
from django_google_places_autocomplete import (
    GooglePlacesAutocompleteWidget,
    require_places_selection,
)

class MyForm(forms.ModelForm):
    address_place_id = forms.CharField(required=False, widget=forms.HiddenInput())

    class Meta:
        model = MyModel
        fields = '__all__'
        widgets = {
            'address': GooglePlacesAutocompleteWidget(require_place_selection=True),
        }

    def clean(self):
        cleaned_data = super().clean()
        require_places_selection(self, 'address')
        return cleaned_data

If the user types manually, the hidden place_id is cleared and validation fails when the text box is non-empty.

Note: This check is based on the submitted place_id. It improves UX discipline; for high-assurance verification you would validate place_id server-side with the Places API.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_google_places_autocomplete-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file django_google_places_autocomplete-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_google_places_autocomplete-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f75c7770b2f8ec3e0b9608777d324c6533dd20f30b060370fae13853204fd640
MD5 7a25be4910023848acdfcf0ae0282193
BLAKE2b-256 6a053dcf789454944af2312f9ee059846dd0eaca477a1b8625c7dc02d153a5e6

See more details on using hashes here.

File details

Details for the file django_google_places_autocomplete-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_google_places_autocomplete-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5faee6158b3ecce7685295a90acefab7af41fe75bbf2ef9ee30685a0c0e880b1
MD5 fe3ae01a138c7e1ad01bcaa2b97f1120
BLAKE2b-256 f31ed216acc02bb43fd501151582d1b87d2b197a893d7ccd839f3179e8e5bcfb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page