Access Django Constants from in a Template

Often times you’ll find that you need to use a constant that’s defined in your Django settings within a template. Unfortunately Django only provides access to a handful of settings by default. Anything beyond that small set, including custom constants that you have defined, are not accessible.

The easiest way to deal with this problem is to make use of a context processor. First, create a new file called within your application:

In this example I am making two constants available within my templates – GOOGLE_ANALYTICS and GOOGLE_API_KEY.

Next, add this new context processor to your settings:

Now you can make use of the constant from within your template:

2 thoughts on “Access Django Constants from in a Template”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s