1

I'm triyng to send two variables from settings.py to a template. At the end of settings.py I've putted that variables:

settings.py

.
.
.
# MapBox Studio
mapbox_user = 'a_string'
mapbox_token = 'a_string'

Then I've created a context_processors:

context_processors.py

from django.conf import settings

def mapbox_data(request):
    mapbox_user = settings.mapbox_user
    mapbox_token = settings.mapbox_token
    context = {
        'mapbox_user': mapbox_user,
        'mapbox_token': mapbox_token,
    }
    return context

And at the end I've putted it inside context_processors list:

    'context_processors': [
        'django.template.context_processors.debug',
        'django.template.context_processors.request',
        'django.contrib.auth.context_processors.auth',
        'django.contrib.messages.context_processors.messages',
        # project context
        'webgis.context_processors.mapbox_data',
    ],

Now I use {{ mapbox_user }} and {{ mapbox_token }} inside my template.

var outDoors = new ol.layer.Tile({
  type: 'base',
  title: 'Topographic',
  visible: true,
  source: new ol.source.XYZ({
      attributions: 'powered with <a href="https://openlayers.org/" target="_blank">OpenLayers</a>',
      url: 'https://api.mapbox.com/styles/v1/{{ mapbox_user }}/'
          + 'cjxkimp5j5s0o1ct4b68n4x1p/tiles/256/{z}/{x}/{y}?'
          + 'access_token={{ mapbox_token }}'
    }),
});

But I see this error:

AttributeError: 'Settings' object has no attribute 'mapbox_user'

It is the first time that I try to pass data from settings.py to a template. What I've wrong? If I use mapbox_user and mapbox_token directly into HTML I can see my map without problems.

MaxDragonheart
  • 1,117
  • 13
  • 34

1 Answers1

0

You need to declare the variable in upper case in the settings.py file.

Use:

MAPBOX_USER = 'a_string'
MAPBOX_TOKEN = 'a_string'
Rakesh
  • 81,458
  • 17
  • 76
  • 113