forms-api.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ===================
  2. GeoDjango Forms API
  3. ===================
  4. .. module:: django.contrib.gis.forms
  5. :synopsis: GeoDjango forms API.
  6. GeoDjango provides some specialized form fields and widgets in order to visually
  7. display and edit geolocalized data on a map. By default, they use
  8. `OpenLayers`_-powered maps, with a base WMS layer provided by `NASA`_.
  9. .. _OpenLayers: https://openlayers.org/
  10. .. _NASA: https://www.earthdata.nasa.gov/
  11. Field arguments
  12. ===============
  13. In addition to the regular :ref:`form field arguments <core-field-arguments>`,
  14. GeoDjango form fields take the following optional arguments.
  15. ``srid``
  16. --------
  17. .. attribute:: Field.srid
  18. This is the SRID code that the field value should be transformed to. For
  19. example, if the map widget SRID is different from the SRID more generally
  20. used by your application or database, the field will automatically convert
  21. input values into that SRID.
  22. ``geom_type``
  23. -------------
  24. .. attribute:: Field.geom_type
  25. You generally shouldn't have to set or change that attribute which should
  26. be set up depending on the field class. It matches the OpenGIS standard
  27. geometry name.
  28. Form field classes
  29. ==================
  30. ``GeometryField``
  31. -----------------
  32. .. class:: GeometryField
  33. ``PointField``
  34. --------------
  35. .. class:: PointField
  36. ``LineStringField``
  37. -------------------
  38. .. class:: LineStringField
  39. ``PolygonField``
  40. ----------------
  41. .. class:: PolygonField
  42. ``MultiPointField``
  43. -------------------
  44. .. class:: MultiPointField
  45. ``MultiLineStringField``
  46. ------------------------
  47. .. class:: MultiLineStringField
  48. ``MultiPolygonField``
  49. ---------------------
  50. .. class:: MultiPolygonField
  51. ``GeometryCollectionField``
  52. ---------------------------
  53. .. class:: GeometryCollectionField
  54. Form widgets
  55. ============
  56. .. module:: django.contrib.gis.forms.widgets
  57. :synopsis: GeoDjango widgets API.
  58. GeoDjango form widgets allow you to display and edit geographic data on a
  59. visual map.
  60. Note that none of the currently available widgets supports 3D geometries, hence
  61. geometry fields will fallback using a ``Textarea`` widget for such data.
  62. Widget attributes
  63. -----------------
  64. GeoDjango widgets are template-based, so their attributes are mostly different
  65. from other Django widget attributes.
  66. .. attribute:: BaseGeometryWidget.geom_type
  67. The OpenGIS geometry type, generally set by the form field.
  68. .. attribute:: BaseGeometryWidget.map_srid
  69. SRID code used by the map (default is 4326).
  70. .. attribute:: BaseGeometryWidget.display_raw
  71. Boolean value specifying if a textarea input showing the serialized
  72. representation of the current geometry is visible, mainly for debugging
  73. purposes (default is ``False``).
  74. .. attribute:: BaseGeometryWidget.supports_3d
  75. Indicates if the widget supports edition of 3D data (default is ``False``).
  76. .. attribute:: BaseGeometryWidget.template_name
  77. The template used to render the map widget.
  78. You can pass widget attributes in the same manner that for any other Django
  79. widget. For example::
  80. from django.contrib.gis import forms
  81. class MyGeoForm(forms.Form):
  82. point = forms.PointField(widget=forms.OSMWidget(attrs={"display_raw": True}))
  83. Widget classes
  84. --------------
  85. ``BaseGeometryWidget``
  86. .. class:: BaseGeometryWidget
  87. This is an abstract base widget containing the logic needed by subclasses.
  88. You cannot directly use this widget for a geometry field.
  89. Note that the rendering of GeoDjango widgets is based on a template,
  90. identified by the :attr:`template_name` class attribute.
  91. ``OpenLayersWidget``
  92. .. class:: OpenLayersWidget
  93. This is the default widget used by all GeoDjango form fields.
  94. ``template_name`` is ``gis/openlayers.html``.
  95. ``OpenLayersWidget`` and :class:`OSMWidget` use the ``ol.js`` file hosted
  96. on the ``cdn.jsdelivr.net`` content-delivery network. You can subclass
  97. these widgets in order to specify your own version of the ``ol.js`` file in
  98. the ``js`` property of the inner ``Media`` class (see
  99. :ref:`assets-as-a-static-definition`).
  100. ``OSMWidget``
  101. .. class:: OSMWidget
  102. This widget uses an OpenStreetMap base layer to display geographic objects
  103. on. Attributes are:
  104. .. attribute:: template_name
  105. ``gis/openlayers-osm.html``
  106. .. attribute:: default_lat
  107. .. attribute:: default_lon
  108. The default center latitude and longitude are ``47`` and ``5``,
  109. respectively, which is a location in eastern France.
  110. .. attribute:: default_zoom
  111. The default map zoom is ``12``.
  112. The :class:`OpenLayersWidget` note about JavaScript file hosting above also
  113. applies here. See also this `FAQ answer`_ about ``https`` access to map
  114. tiles.
  115. .. _FAQ answer: https://help.openstreetmap.org/questions/10920/how-to-embed-a-map-in-my-https-site