How to do it...

Go through the following steps:

  1. Register for the Google Maps API key. You can learn how and where to do this at the Google developers' documentation at https://developers.google.com/maps/documentation/javascript/get-api-key.
  1. Add the Google Maps API key to the secrets and then read it out in the settings:
# myproject/settings/_base.py# …GOOGLE_MAPS_API_KEY = get_secret("GOOGLE_MAPS_API_KEY")
  1. At the core app, create a context processor to expose GOOGLE_MAPS_API_KEY to the templates:
# myproject/apps/core/context_processors.pyfrom django.conf import settingsdef google_maps(request):    return {        "GOOGLE_MAPS_API_KEY": settings.GOOGLE_MAPS_API_KEY,    }
  1. Refer to this context processor in the template settings:
# myproject/settings/_base.py ...

Get Django 3 Web Development Cookbook - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.