2013-03-16 18:39:18 +08:00
|
|
|
===================
|
|
|
|
GeoDjango Forms API
|
|
|
|
===================
|
|
|
|
|
|
|
|
.. module:: django.contrib.gis.forms
|
2016-02-19 15:31:25 +08:00
|
|
|
:synopsis: GeoDjango forms API.
|
2013-03-16 18:39:18 +08:00
|
|
|
|
2013-10-11 04:42:30 +08:00
|
|
|
GeoDjango provides some specialized form fields and widgets in order to visually
|
2013-03-16 18:39:18 +08:00
|
|
|
display and edit geolocalized data on a map. By default, they use
|
2017-05-20 23:51:21 +08:00
|
|
|
`OpenLayers`_-powered maps, with a base WMS layer provided by `NASA`_.
|
2013-03-16 18:39:18 +08:00
|
|
|
|
2018-01-07 21:28:41 +08:00
|
|
|
.. _OpenLayers: https://openlayers.org/
|
2017-05-20 23:51:21 +08:00
|
|
|
.. _NASA: https://earthdata.nasa.gov/
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
Field arguments
|
|
|
|
===============
|
|
|
|
In addition to the regular :ref:`form field arguments <core-field-arguments>`,
|
|
|
|
GeoDjango form fields take the following optional arguments.
|
|
|
|
|
|
|
|
``srid``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.srid
|
|
|
|
|
|
|
|
This is the SRID code that the field value should be transformed to. For
|
|
|
|
example, if the map widget SRID is different from the SRID more generally
|
|
|
|
used by your application or database, the field will automatically convert
|
|
|
|
input values into that SRID.
|
|
|
|
|
|
|
|
``geom_type``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.geom_type
|
|
|
|
|
|
|
|
You generally shouldn't have to set or change that attribute which should
|
|
|
|
be setup depending on the field class. It matches the OpenGIS standard
|
|
|
|
geometry name.
|
|
|
|
|
|
|
|
Form field classes
|
|
|
|
==================
|
|
|
|
|
|
|
|
``GeometryField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: GeometryField
|
|
|
|
|
|
|
|
``PointField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: PointField
|
|
|
|
|
|
|
|
``LineStringField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: LineStringField
|
|
|
|
|
|
|
|
``PolygonField``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: PolygonField
|
|
|
|
|
|
|
|
``MultiPointField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: MultiPointField
|
|
|
|
|
|
|
|
``MultiLineStringField``
|
2016-01-03 18:56:22 +08:00
|
|
|
------------------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: MultiLineStringField
|
|
|
|
|
|
|
|
``MultiPolygonField``
|
2016-01-03 18:56:22 +08:00
|
|
|
---------------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: MultiPolygonField
|
|
|
|
|
|
|
|
``GeometryCollectionField``
|
2016-01-03 18:56:22 +08:00
|
|
|
---------------------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. class:: GeometryCollectionField
|
|
|
|
|
|
|
|
Form widgets
|
|
|
|
============
|
|
|
|
|
2017-05-12 21:22:59 +08:00
|
|
|
.. module:: django.contrib.gis.forms.widgets
|
2016-02-19 15:31:25 +08:00
|
|
|
:synopsis: GeoDjango widgets API.
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
GeoDjango form widgets allow you to display and edit geographic data on a
|
|
|
|
visual map.
|
|
|
|
Note that none of the currently available widgets supports 3D geometries, hence
|
2019-06-17 22:54:55 +08:00
|
|
|
geometry fields will fallback using a ``Textarea`` widget for such data.
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
Widget attributes
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
GeoDjango widgets are template-based, so their attributes are mostly different
|
|
|
|
from other Django widget attributes.
|
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: BaseGeometryWidget.geom_type
|
|
|
|
|
|
|
|
The OpenGIS geometry type, generally set by the form field.
|
|
|
|
|
|
|
|
.. attribute:: BaseGeometryWidget.map_height
|
|
|
|
.. attribute:: BaseGeometryWidget.map_width
|
|
|
|
|
|
|
|
Height and width of the widget map (default is 400x600).
|
|
|
|
|
|
|
|
.. attribute:: BaseGeometryWidget.map_srid
|
|
|
|
|
|
|
|
SRID code used by the map (default is 4326).
|
|
|
|
|
2013-08-30 16:48:36 +08:00
|
|
|
.. attribute:: BaseGeometryWidget.display_raw
|
2013-03-16 18:39:18 +08:00
|
|
|
|
2013-08-30 16:48:36 +08:00
|
|
|
Boolean value specifying if a textarea input showing the serialized
|
|
|
|
representation of the current geometry is visible, mainly for debugging
|
|
|
|
purposes (default is ``False``).
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
.. attribute:: BaseGeometryWidget.supports_3d
|
|
|
|
|
|
|
|
Indicates if the widget supports edition of 3D data (default is ``False``).
|
|
|
|
|
|
|
|
.. attribute:: BaseGeometryWidget.template_name
|
|
|
|
|
|
|
|
The template used to render the map widget.
|
|
|
|
|
|
|
|
You can pass widget attributes in the same manner that for any other Django
|
|
|
|
widget. For example::
|
|
|
|
|
|
|
|
from django.contrib.gis import forms
|
|
|
|
|
|
|
|
class MyGeoForm(forms.Form):
|
|
|
|
point = forms.PointField(widget=
|
|
|
|
forms.OSMWidget(attrs={'map_width': 800, 'map_height': 500}))
|
|
|
|
|
|
|
|
Widget classes
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
``BaseGeometryWidget``
|
|
|
|
|
|
|
|
.. class:: BaseGeometryWidget
|
|
|
|
|
|
|
|
This is an abstract base widget containing the logic needed by subclasses.
|
|
|
|
You cannot directly use this widget for a geometry field.
|
|
|
|
Note that the rendering of GeoDjango widgets is based on a template,
|
|
|
|
identified by the :attr:`template_name` class attribute.
|
|
|
|
|
|
|
|
``OpenLayersWidget``
|
|
|
|
|
|
|
|
.. class:: OpenLayersWidget
|
|
|
|
|
|
|
|
This is the default widget used by all GeoDjango form fields.
|
|
|
|
``template_name`` is ``gis/openlayers.html``.
|
|
|
|
|
2013-12-28 18:08:50 +08:00
|
|
|
``OpenLayersWidget`` and :class:`OSMWidget` use the ``openlayers.js`` file
|
2016-08-10 23:13:08 +08:00
|
|
|
hosted on the ``cdnjs.cloudflare.com`` content-delivery network. You can
|
|
|
|
subclass these widgets in order to specify your own version of the
|
2019-08-21 16:57:24 +08:00
|
|
|
``OpenLayers.js`` file in the ``js`` property of the inner ``Media`` class
|
|
|
|
(see :ref:`assets-as-a-static-definition`).
|
2013-12-28 18:08:50 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
``OSMWidget``
|
|
|
|
|
|
|
|
.. class:: OSMWidget
|
|
|
|
|
2015-05-25 23:31:26 +08:00
|
|
|
This widget uses an OpenStreetMap base layer to display geographic objects
|
2017-05-12 23:24:53 +08:00
|
|
|
on. Attributes are:
|
|
|
|
|
|
|
|
.. attribute:: template_name
|
|
|
|
|
|
|
|
``gis/openlayers-osm.html``
|
|
|
|
|
|
|
|
.. attribute:: default_lat
|
|
|
|
.. attribute:: default_lon
|
|
|
|
|
|
|
|
The default center latitude and longitude are ``47`` and ``5``,
|
|
|
|
respectively, which is a location in eastern France.
|
2013-12-28 18:08:50 +08:00
|
|
|
|
2017-05-15 02:31:17 +08:00
|
|
|
.. attribute:: default_zoom
|
|
|
|
|
|
|
|
The default map zoom is ``12``.
|
|
|
|
|
2013-12-28 18:08:50 +08:00
|
|
|
The :class:`OpenLayersWidget` note about JavaScript file hosting above also
|
|
|
|
applies here. See also this `FAQ answer`_ about ``https`` access to map
|
|
|
|
tiles.
|
|
|
|
|
|
|
|
.. _FAQ answer: https://help.openstreetmap.org/questions/10920/how-to-embed-a-map-in-my-https-site
|