Plugs Google Maps Platform API into Django admin.
Project description
django-google-maps-widget is a simple library that provides the basic hooks into Google Maps Platform for use in Django models from Django version 4.2+. Supports Python 3.9+. Forked from this project originally authored by @madisona, with adaptation to the modern Python environment.
This library is useful to allow someone from the admin panels to type a freeform address, have the address geocoded on change and plotted on the map. If the location is not 100% correct, the user can drag the marker to the correct spot and the geo coordinates will update.
Usage
Include the django_google_maps_widget app in your settings.py
Add your Google Maps API Key in your settings.py as GOOGLE_MAPS_API_KEY
Add your Google Maps map ID in your settings.py as GOOGLE_MAPS_MAP_ID (recommended). Map ID is necessary for the map to function and is injected via field attribute, but you can provide this attribute however you want.
Create a model that has both an address field and geolocation field:
from django.db import models from django_google_maps_widget import fields as map_fields class Rental(models.Model): address = map_fields.AddressField(max_length=200) geolocation = map_fields.GeoLocationField(max_length=100)In the admin.py, include the following as a formfield_override:
from django.contrib import admin from django_google_maps_widget import widgets as map_widgets from django_google_maps_widget import fields as map_fields class RentalAdmin(admin.ModelAdmin): formfield_overrides = { map_fields.AddressField: { "widget": map_widgets.GoogleMapsAddressWidget(attrs={ "mapid": settings.GOOGLE_MAPS_MAP_ID }) }, }To change the map type (hybrid by default), you can add an html attribute on the AddressField widget. The list of allowed values is: hybrid, roadmap, satellite, terrain
from django.contrib import admin from django_google_maps_widget import widgets as map_widgets from django_google_maps_widget import fields as map_fields class RentalAdmin(admin.ModelAdmin): formfield_overrides = { map_fields.AddressField: { "widget": map_widgets.GoogleMapsAddressWidget(attrs={"data-map-type": "roadmap"})}, }To change the autocomplete options, you can add an html attribute on the AddressField widget. See https://developers.google.com/maps/documentation/javascript/places-autocomplete#add_autocomplete for a list of available options
import json from django.contrib import admin from django_google_maps_widget import widgets as map_widgets from django_google_maps_widget import fields as map_fields class RentalAdmin(admin.ModelAdmin): formfield_overrides = { map_fields.AddressField: { "widget": map_widgets.GoogleMapsAddressWidget(attrs={ "data-autocomplete-options": json.dumps({ "types": ["geocode", "establishment"], "componentRestrictions": { "country": "us" } }) }) }, }
That should be all you need to get started.
It can be useful to make the geolocation field readonly in the admin so a user doesn’t accidentally change it to a nonsensical value. There is validation on the field, so you can’t enter an incorrect value, but you could enter something that is not even close to the address you intended.
When you’re displaying the address back to the user, just request the map using the geocoordinates that were saved in your model.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_google_maps_widget-1.0.0.tar.gz.
File metadata
- Download URL: django_google_maps_widget-1.0.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1af5aa1d1942f5e440a02202b582af47991a6c5a57a2ca90fcd1988b8cd16411
|
|
| MD5 |
3c25fa318446d1744f9365a3d8332177
|
|
| BLAKE2b-256 |
efe7db3c50e7536a21a37ae5d554afdc3e27c22cf24a919391be9f7f1fe7fba3
|
Provenance
The following attestation bundles were made for django_google_maps_widget-1.0.0.tar.gz:
Publisher:
python-publish.yml on amv-bamboo/django-google-maps-widget
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_google_maps_widget-1.0.0.tar.gz -
Subject digest:
1af5aa1d1942f5e440a02202b582af47991a6c5a57a2ca90fcd1988b8cd16411 - Sigstore transparency entry: 153095873
- Sigstore integration time:
-
Permalink:
amv-bamboo/django-google-maps-widget@7a7af34826a398228804eff6990220811b6e7a6f -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/amv-bamboo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7a7af34826a398228804eff6990220811b6e7a6f -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_google_maps_widget-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_google_maps_widget-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c09d6c00a072f847d1a4c9cca4e0a1f172f82ce6b2b11faebb3f56aaa4362290
|
|
| MD5 |
7cbf180bd361b285166559dbbe392830
|
|
| BLAKE2b-256 |
6ce622c8904e9a12734d690a68c60e10577290efd0054cc445c9a6aeada3175b
|
Provenance
The following attestation bundles were made for django_google_maps_widget-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on amv-bamboo/django-google-maps-widget
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_google_maps_widget-1.0.0-py3-none-any.whl -
Subject digest:
c09d6c00a072f847d1a4c9cca4e0a1f172f82ce6b2b11faebb3f56aaa4362290 - Sigstore transparency entry: 153095876
- Sigstore integration time:
-
Permalink:
amv-bamboo/django-google-maps-widget@7a7af34826a398228804eff6990220811b6e7a6f -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/amv-bamboo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7a7af34826a398228804eff6990220811b6e7a6f -
Trigger Event:
release
-
Statement type: