5

Django: how to use settings in templates?

Bill Paetzke
  • 13,332
  • 6
  • 44
  • 46
user469652
  • 48,855
  • 59
  • 128
  • 165

4 Answers4

13

You can pass settings to a template like this:

from django.conf import settings
from django.template import RequestContext

def index(request):
        return render_to_response('index.html', {'settings': settings},
        context_instance=RequestContext(request))

In the template:

{{ settings.MY_SETTING_NAME }}

If you need to access your settings in many templates (many views), consider creating an appropriate template context processor.

Arseny
  • 5,159
  • 4
  • 21
  • 24
2

If using a class-based view:

#
# in settings.py
#
YOUR_CUSTOM_SETTING = 'some value'

#
# in views.py
#
from django.conf import settings #for getting settings vars

class YourView(DetailView): #assuming DetailView; whatever though

    # ...

    def get_context_data(self, **kwargs):

        context = super(YourView, self).get_context_data(**kwargs)
        context['YOUR_CUSTOM_SETTING'] = settings.YOUR_CUSTOM_SETTING

        return context

#
# in your_template.html, reference the setting like any other context variable
#
{{ YOUR_CUSTOM_SETTING }}
Bill Paetzke
  • 13,332
  • 6
  • 44
  • 46
0

Just import it.

from django.conf import settings
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

Really simple.

Like this:

from django.conf import settings

print settings.MY_SETTING_NAME
Arseny
  • 5,159
  • 4
  • 21
  • 24