How to tweak GeoDjango Admin

2 minutes read

In my GeoDjango tutorial series, I shared details on the development of a web-mapping solution using GeoDjango. Depending on the structure of the application and the use, Django admin is a very useful feature in this framework. There is power in the admin.

In this post, I will share some tips on how to tweak your GeoDjango admin to enhance display, look and feel and functionality. By default, Django comes with OSMGeoAdmin which is an extension used with spatial models. We want to replace this option with other widgets and tools that are even better.

Django Leaflet

For us to use Leaflet in our project admin, we need to install it. To install Leaflet, run

This will install the latest version of Leaflet in your Django project. Once installed, edit your admin.py to the following(Depending on your models)

#admin.py

In this scenario, we use Leaflet for the admin section(spatial models) in the project. LeafletGeoAdmin will output an interface as the one shown below;

Spot the difference?

The LeafletGeoAdmin looks way much better, sleek and has more tools than OSMGeoAdmin.

One more thing;

Try adding a new record of the Incidence model from the admin. You will not that the geo field is zoomed to [0,0] by default and displays multiple worlds(If there is something like this). It also doesn’t limit the zoom levels on the map.

Let’s work on this. To customize this bit, let’s add a configuration in our settings.py file. Add this code to the bottom;

 

Django Map Widgets

As earlier indicated, we can make our Django admin even more beautiful and flexible using a number of tools. Map widgets is one of these tools.

Currently, only Google Maps services are supported. I look forward to the support of other services that exist out there.

To install Django Map Widgets, run the command;

Adjust your models.py file as below;

Note we have the class has admin.ModelAdmin. Google Map Widgets is a good option if you don’t want to change the base class in your admin declarations.

For this widget, remember to collectstatic so as to fetch the static files into your project. To collect static, run

Refresh your admin and check the changes

Using this widget, it offers options to enter lat and long, using your location, dragging a marker to your point of interest and searching for an address of the point of interest.

You can try to workout a model with other types of data such as polylines and areas.

Well, I think we’ve done much tweaking on our GeoDjango admin.

What other methods do you use to customize your GeoDjango admin? Share with us.

Keep posted for more posts.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: