Package: django-admin-geomap
DjangoAdminGeomap libraryIn RussianThe free, open-source DjangoAdminGeomap library is designed to display objects on the map in the Django views and admin site.There is a full-fledged multifunctional GIS frameworkGeoDjango.
When is used in the Django admin site, you can display objects on the map.
However, GeoDjango has a largelist of dependencieson various libraries
and the specifics of installing these libraries on various platforms.If you only need to display objects on the map in the Django admin site, then you can use the DjangoAdminGeomap library.
It has no additional requirements for the names and data types of fields in the database tables, and there are no installation dependencies.DjangoAdminGeomap uses theOpenLayersJavaScript framework to display map data.
The source of the cartographic data is theOpenStreetMap project.Installationpipinstalldjango-admin-geomapTo connect DjangoAdminGeomap to your project, add'django_admin_geomap'to theINSTALLED_APPSin yoursettings.pyfile.INSTALLED_APPS=(...'django_admin_geomap',)Initial dataLet's say we have a table in the database. The records contain data about coordinates.# models.pyfromdjango.dbimportmodelsclassLocation(models.Model):name=models.CharField(max_length=100)lon=models.FloatField()# longitudelat=models.FloatField()# latitudeOn the main page of the site and when working with this table in the admin panel, we want to see objects from this table located on the map.Main page with a list of objects on the mapTo enable the display ofLocationobjects on the map, you need to make changes to the model class in themodels.pyfile.Add thedjango_admin_geomap.GeoItem"mixin" class to the inheritance list of theLocationclass and define two properties:geomap_longitudeandgeomap_latitude.
These properties should return the longitude and latitude of the object as a string.If at least one of these two properties returns an empty string, then the corresponding object will not be displayed on the map.
For example, objects that do not have coordinates or some kind of "secret" objects from your database.# models.pyfromdjango.dbimportmodelsfromdjango_admin_geomapimportGeoItemclassLocation(models.Model,GeoItem):@propertydefgeomap_longitude(self):return''ifself.lonisNoneelsestr(self.lon)@propertydefgeomap_latitude(self):return''ifself.latisNoneelsestr(self.lat)After making these changes to the definition of the model, you can display a map with objects from theLocationtable in an arbitrary view.
To do this, you need to include the filegeomap/common.htmlin the page template. For example, the site root page templatehome.htmlmight look like this:<!DOCTYPE html><htmllang="en"><head><title>DjangoAdminGeomap example</title></head><body>Hello, OpenStreetMap!<div>{% include "geomap/common.html" %}</div></body></html>In the view function, you need to pass to this template the context formed by calling thegeomap_contextfunction.
As a required argument to the function, you need to pass an iterable sequence of objects derived from the classdjango_admin_geomap.GeoItemto display on the map.
For example a list or Django QuerySet.# views.pyfromdjango.shortcutsimportrenderfromdjango_admin_geomapimportgeomap_contextfrom.modelsimportLocationdefhome(request):returnrender(request,'home.html',geomap_context(Location.objects.all()))On the root page of the site, a map with markers in the locations of these objects will be displayed.Thegeomap_contextfunction accepts additional named arguments to customize the properties of the map.map_longitude: map center longitude, default is "0.0"map_latitude: map center latitude, default is "0.0"map_zoom: map zoom level, default is "1"auto_zoom: enables autozoom mode (see below), default is "-1" (autozoom mode is disabled)map_height: vertical map size, default is "500px"List of objects on the map in the admin panelTo display a map with objects in the site admin panel in the admin settings fileadmin.py, when registering a model, you need to use thedjango_admin_geomap.ModelAdminclass.# admin.pyfromdjango.contribimportadminfromdjango_admin_geomapimportModelAdminfrom.modelsimportLocationadmin.site.register(Location,ModelAdmin)After making these changes, in the admin panel on the page with a list ofLocationobjects, a map with markers at the locations of these objects will be displayed under the table.Displaying the object on the map in the edit form in the admin panelTo display an object on the map in the edit/view form, you must additionally specify the field IDs in the Django form, which contain the longitude and latitude values of the object.For ourLocationclass, the Django admin automatically assigns the IDsid_lonandid_latto these form fields. The following changes need to be made to theadmin.pyfile.# admin.pyfromdjango.contribimportadminfromdjango_admin_geomapimportModelAdminfrom.modelsimportLocationclassAdmin(ModelAdmin):geomap_field_longitude="id_lon"geomap_field_latitude="id_lat"admin.site.register(Location,Admin)After making these changes, in the admin panel on the page for viewing/editing theLocationobject, a map with a marker at the location of the object will be displayed.When editing, you can change the position of an object by dragging its icon across the map with the mouse (you need to move the mouse cursor over the bottom of the icon until a blue dot appears on it).When adding a new object, its position can be set by clicking on the map. Further, the marker of the new object can be dragged, similar to editing.Autozoom modeBy default, this mode is disabled.
You can enable autozoom mode when displaying objects on the map both in regular views and in the Django admin panel.In regular Django views, thegeomap_contextfunction needs to be passed the named argumentauto_zoom.returnrender(request,'home.html',geomap_context(Location.objects.all(),auto_zoom="10"))In the admin panel class, you need to set thegeomap_autozoomattribute.# admin.pyfromdjango_admin_geomapimportModelAdminclassAdmin(ModelAdmin):geomap_autozoom="10"The autozoom mode works differently depending on the number of objects that you want to display on the map.If the list of displayed objects is empty, the autozoom mode is disabled.If the list contains one object, then the map center is set to the coordinates of this object, and the map scale is set to the value of the autozoom parameter (10 for the examples above).If the list contains more than one object, the program determines the minimum rectangle that contains all the displayed objects.
The center of the map is set to the coordinates of the center of this rectangle.
The scale of the map is set in such a way as to contain the given rectangle with some indents along the edges.Additional customizationThe library allows you to customize the view of the map and objects by setting special properties for the model class and thedjango_admin_geomap.ModelAdminclass.Object icon on the mapThegeomap_iconproperty of the model class sets the path to the marker icon. You can use different icons depending on the state of a particular object.The default ishttps://maps.google.com/mapfiles/ms/micons/red.png.# models.pyfromdjango.dbimportmodelsfromdjango_admin_geomapimportGeoItemclassLocation(models.Model,GeoItem):@propertydefgeomap_icon(self):returnself.default_iconText in a pop-up panel when you click on a marker on the mapWhen you click on a marker on the map, a pop-up panel is displayed. The HTML code used in this panel can be set by defining three properties on the model class.geomap_popup_commondisplayed in regular viewsgeomap_popup_viewdisplayed in the admin panel for a user without permission to edit the objectgeomap_popup_editdisplayed in the admin panel for a user who has permission to editBy default, all these properties return the string representation of the object.# models.pyfromdjango.dbimportmodelsfromdjango_admin_geomapimportGeoItemclassLocation(models.Model,GeoItem):@propertydefgeomap_popup_view(self):return"<strong>{}</strong>".format(str(self))@propertydefgeomap_popup_edit(self):returnself.geomap_popup_view@propertydefgeomap_popup_common(self):returnself.geomap_popup_viewNew object iconThegeomap_new_feature_iconproperty of thedjango_admin_geomap.ModelAdminclass sets the path to the marker icon when adding a new object in the admin panel.# admin.pyfromdjango_admin_geomapimportModelAdminclassAdmin(ModelAdmin):geomap_new_feature_icon="/myicon.png"Default map zoom level and center of the map when displaying a list of objects in the admin panelYou can change the zoom level and position of the center of the map by setting the propertiesgeomap_default_longitude,geomap_default_latitudeandgeomap_default_zoomin the classdjango_admin_geomap.ModelAdmin.By default, the center of the map is located at the point with coordinates "0.0", "0.0" and the scale is "1".# admin.pyfromdjango_admin_geomapimportModelAdminclassAdmin(ModelAdmin):geomap_default_longitude="95.1849"geomap_default_latitude="64.2637"geomap_default_zoom="3"Default map zoom level when editing/viewing an object in the admin panelIn object edit form the center of the map coincides with the location of the object. The zoom level of the map can be set by using thegeomap_item_zoomproperty of thedjango_admin_geomap.ModelAdminclass.The default is "13".# admin.pyfromdjango_admin_geomapimportModelAdminclassAdmin(ModelAdmin):geomap_item_zoom="10"Vertical map size in the admin panelWhen displayed, the map occupies the maximum possible horizontal size. The vertical size can be set via thegeomap_heightproperty of thedjango_admin_geomap.ModelAdminclass.
The value must be a string valid in the CSS style definition.The default is "500px".# admin.pyfromdjango_admin_geomapimportModelAdminclassAdmin(ModelAdmin):geomap_height="300px"Show or hide map in list viewBy default, the map is shown on the list view and in the detail view.
To hide the map on the list view, setgeomap_show_map_on_listproperty fromdjango_admin_geomap.ModelAdminclass toFalse.# admin.pyfromdjango_admin_geomapimportModelAdminclassAdmin(ModelAdmin):geomap_show_map_on_list=FalseUsage exampleYou can run an example of using the library on your local host.On the Windows platform, you must first install the following programs.Python3GNUUnix Utilsfor operations via makefileGit for Windowsto access the source code repository.Then clone the repository and run the installation, specifying the path to Python 3.gitclonegit@github.com:vb64/django.admin.geomap.gitcddjango.admin.geomap
makesetupPYTHON_BIN=/usr/bin/python3Collect media files and create a database.makestatic
makedbCreate a database superuser by specifying a login and password for it.makesuperuserRun example.makeexampleOpenhttp://127.0.0.1:8000/in a browser to view the example site.
To enter the admin panelhttp://127.0.0.1:8000/admin/you need to use the login and password that were set when creating the superuser.