Django Google Maps Example Tutorial

Published On: 26/08/2022 | Category: Django

Hi Dev,

Today, I would like to show you django google maps example tutorial. step by step explain django google maps location. you'll learn how to add google map in django. it's simple example of how to use google map in django. Alright, let’s dive into the steps.

In this example, we will create one simple urls and display google map with marker. We will use the google maps js library for adding google map. you can easily add a google map in django all versions.

you can see bellow preview, how it looks:

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2: Create a App

Now we'll create a single app called core to display our google maps. Stop the local server with Control+c and use the startapp command to create this new app.

python3 startapp core
Step 3: Update

In this step we require to do here, do not forget to register the new app in the file. under installed apps, just add ‘core’ to the list:

And, here, we will add new variable in file fo set GOOGLE_MAPS_API_KEY. so let's add as bellow:

Next, you need to add it in the file as follows:




Step 4: Creating the Views

In this step, we need to configure our five views. The maps_view page will just be a template. and pass with google maps api keys open the core/ file and add:

from django.shortcuts import render
from django.conf import settings

def maps_view(request):       
    return render(request, 'googleMap.html', {'google_maps_api_key': settings.GOOGLE_MAPS_API_KEY})
Step 5: Creating the Templates

Next, then with your text editor create new templates files: core/templates/googleMap.html file and the add:

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to Add Google Map in Django? -</title>
    <script src=""></script>
    <style type="text/css">
        #map {
          height: 400px;
    <div class="container mt-5">
        <h2>How to Add Google Map in Django? -</h2>
        <div id="map"></div>
    <script type="text/javascript">
        function initMap() {
          const myLatLng = { lat: 22.2734719, lng: 70.7512559 };
          const map = new google.maps.Map(document.getElementById("map"), {
            zoom: 5,
            center: myLatLng,
          new google.maps.Marker({
            position: myLatLng,
            title: "Hello Rajkot!",
        window.initMap = initMap;
    <script type="text/javascript"
        src="{{ google_maps_api_key }}&callback=initMap" ></script>
Step 6: Creating URLs

In this section, we’ll create the urls to access our views.Go to the core/ file and update it as follows:

from django.urls import path
from core import views

urlpatterns = [
    path('map/', views.maps_view, name="maps_view"),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/map address with a web browser.

I Hope It will help you....