Skip to main content

Improved widgets for Django's PointField

Project description

django-osm-widgets

Improved widgets for Django's PointField.

LatLonOpenlayersOSMWidget handles latitude and longitude inputs synced with the point on the map.

Example of the LatLonOpenlayersOSMWidget on a page

Requirements

  • Python 3.10+
  • Django >=3.0, <5.2

Installation

  • run pip install django-osm-widgets
  • add django_osm_widgets to your INSTALLED_APPS

Usage

In your forms, use the widget like this:

from django.contrib.gis.forms.fields import PointField
from django_osm_widgets.widgets import LatLonOpenlayersOSMWidget


class MyForm(forms.Form):
  location = PointField(widget=LatLonOpenlayersOSMWidget)

The latitute and longitude fields will be automatically added in your page.

Currently django-osm-widgets supports only a unique instance of the LatLonOpenlayersOSMWidget in a page.

Customizations

You can define some options as in the example below.

When using "must_display_latlon_fields": False, your are responsible for providing two input fields in your page. These fields must have ids corresponding to latitude_field_id and longitude_field_id values (defaults to id_osm_widget_latitude and id_osm_widget_longitude) and must appear in the DOM before the LatLonOpenlayersOSMWidget. To achieve that, you may find useful to override the latlon-openlayers-osm.html template as follow:

{% extends "django_osm_widgets/latlon-openlayers-osm.html" %}

{% block map_wrapper %}
  {{ block.super }}
  <label for="{{ latitude_field_id }}" class="form-label">Latitude</label>
  <input type="number" step="0.0001" min="-90" max="90" name="latitude" id="{{ latitude_field_id }}" placeholder="for example: 45.123456" class="form-control">
  <label for="{{ longitude_field_id }}" class="form-label">Longitude</label>
  <input type="number" step="0.0001" min="-180" max="180" name="longitude" id="{{ longitude_field_id }}" placeholder="for example: 2.123456" class="form-control">
{% endblock map_wrapper %}

You can override some attributes when instantiating the widget class in your form. Below are all the attributes and their default values.

from django.contrib.gis.forms.fields import PointField
from django_osm_widgets.widgets import LatLonOpenlayersOSMWidget


class MyForm(forms.Form):
  location = PointField(
    widget=LatLonOpenlayersOSMWidget(
      attrs={
        "must_display_latlon_fields": True,
        "map_width": "auto",
        "map_height": "auto",
        "default_lat": 45,
        "default_lon": 5,
        "default_zoom": 8,
        "latitude_field_id": "id_osm_widget_latitude",
        "longitude_field_id": "id_osm_widget_longitude",
        "listened_events": "input",
        "marker_options": {
          "src": "https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/images/marker-icon.png",
          "scale": 1,
          "anchor": [0.5, 1],
        },
        "precision": 4,
        "geocoder_address_field_ids": {
          "street": "id_adresse",
          "postal_code": "id_code_postal",
          "city": "id_localite",
          "country": "id_pays",
        },
        "geocoder_provider": "nominatim",
        "clear_features_label": "Delete all Features",
        "geocoder_button_label": "Geocode from address",
        "geocoder_message_timeout": 5000,
      }
    )
  )

Projects can override button labels and the status message timeout:

  • Via widget attrs: Pass clear_features_label, geocoder_button_label, geocoder_message_timeout when instantiating the widget.
  • Via template blocks: Extend latlon-openlayers-osm.html and override {% block clear_features_label %} or {% block geocoder_button_label %} for full control.

Labels support translation when using Django's i18n. The status message (e.g. "Coordinates updated.") disappears automatically after geocoder_message_timeout milliseconds (default 5 seconds).

Geocoding from address

When geocoder_address_field_ids is provided, a "Geocode from address" button appears next to the map. It maps your form's address fields (street, postal_code, city, country) to DOM element IDs. When clicked, the widget fetches coordinates and updates the map.

The geocoding provider is configurable via geocoder_provider:

  • nominatim (default): OpenStreetMap Nominatim. Works worldwide with international addresses.
  • ign: IGN Géoplateforme. Optimized for French addresses (BAN, BD TOPO®, Parcellaire Express). 50 requests/second limit per IP.

Example for French addresses:

"geocoder_provider": "ign",

You can omit any of the four address keys; at least one address field must be filled.

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_osm_widgets-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

django_osm_widgets-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file django_osm_widgets-0.1.0.tar.gz.

File metadata

  • Download URL: django_osm_widgets-0.1.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.1 Linux/6.12.67-linuxkit

File hashes

Hashes for django_osm_widgets-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e4b410f308da8f51b0d92afdf4e579fd930e15fd4daae1ee49ab1b0fe6a1bbc0
MD5 ef6928a94bbb980b9f23fa146dbf6e53
BLAKE2b-256 7693265eb4fec4ebf07f6f791b31a710c681b39060e1ce4a83321544dc6f97f4

See more details on using hashes here.

File details

Details for the file django_osm_widgets-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_osm_widgets-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.1 Linux/6.12.67-linuxkit

File hashes

Hashes for django_osm_widgets-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce3c43fd28596770d9cf20dd97889da8712728e726e2ddf453abdf5da7114a2e
MD5 294cef0d851d5b59e9110c2f39780461
BLAKE2b-256 d6d52695cda2fd6b8e85b3d63d1bcd613c1c6c46ad7ce19f5b6df1069cf3d060

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