forms-api.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_height
  69. .. attribute:: BaseGeometryWidget.map_width
  70. Height and width of the widget map (default is 400x600).
  71. .. deprecated:: 4.2
  72. ``map_height`` and ``map_width`` attributes are deprecated, use CSS to
  73. size map widgets instead.
  74. .. attribute:: BaseGeometryWidget.map_srid
  75. SRID code used by the map (default is 4326).
  76. .. attribute:: BaseGeometryWidget.display_raw
  77. Boolean value specifying if a textarea input showing the serialized
  78. representation of the current geometry is visible, mainly for debugging
  79. purposes (default is ``False``).
  80. .. attribute:: BaseGeometryWidget.supports_3d
  81. Indicates if the widget supports edition of 3D data (default is ``False``).
  82. .. attribute:: BaseGeometryWidget.template_name
  83. The template used to render the map widget.
  84. You can pass widget attributes in the same manner that for any other Django
  85. widget. For example::
  86. from django.contrib.gis import forms
  87. class MyGeoForm(forms.Form):
  88. point = forms.PointField(widget=forms.OSMWidget(attrs={"display_raw": True}))
  89. Widget classes
  90. --------------
  91. ``BaseGeometryWidget``
  92. .. class:: BaseGeometryWidget
  93. This is an abstract base widget containing the logic needed by subclasses.
  94. You cannot directly use this widget for a geometry field.
  95. Note that the rendering of GeoDjango widgets is based on a template,
  96. identified by the :attr:`template_name` class attribute.
  97. ``OpenLayersWidget``
  98. .. class:: OpenLayersWidget
  99. This is the default widget used by all GeoDjango form fields.
  100. ``template_name`` is ``gis/openlayers.html``.
  101. ``OpenLayersWidget`` and :class:`OSMWidget` use the ``ol.js`` file hosted
  102. on the ``cdn.jsdelivr.net`` content-delivery network. You can subclass
  103. these widgets in order to specify your own version of the ``ol.js`` file in
  104. the ``js`` property of the inner ``Media`` class (see
  105. :ref:`assets-as-a-static-definition`).
  106. ``OSMWidget``
  107. .. class:: OSMWidget
  108. This widget uses an OpenStreetMap base layer to display geographic objects
  109. on. Attributes are:
  110. .. attribute:: template_name
  111. ``gis/openlayers-osm.html``
  112. .. attribute:: default_lat
  113. .. attribute:: default_lon
  114. The default center latitude and longitude are ``47`` and ``5``,
  115. respectively, which is a location in eastern France.
  116. .. attribute:: default_zoom
  117. The default map zoom is ``12``.
  118. The :class:`OpenLayersWidget` note about JavaScript file hosting above also
  119. applies here. See also this `FAQ answer`_ about ``https`` access to map
  120. tiles.
  121. .. _FAQ answer: https://help.openstreetmap.org/questions/10920/how-to-embed-a-map-in-my-https-site