28

I want to be able to put certain configuration information in my settings.py file - things like the site name, site url, etc.

If I do this, how can I then access those settings in templates?

Thanks

Bill Paetzke
  • 13,332
  • 6
  • 44
  • 46
Roger
  • 4,911
  • 6
  • 29
  • 26

3 Answers3

54

Let's say in your settings.py file you have:

SITE_URL='www.mydomain.tld/somewhere/'
SITE_NAME='My site'

If you need that in just one or two views:

from django.shortcuts import render_to_response
from django.conf import settings

def my_view(request, ...):
    response_dict = {
        'site_name': settings.SITE_NAME,
        'site_url': settings.SITE_URL,
    }
    ...
    return render_to_response('my_template_dir/my_template.html', response_dict)

If you need to access these across a lot of apps and/or views, you can write a context processor to save code:

James has a tutorial on this online.

Some useful information on the when and if of context processors is available on this very site here.

Inside your my_context_processors.py file you would:

from django.conf import settings

def some_context_processor(request):
    my_dict = {
        'site_url': settings.SITE_URL,
        'site_name': settings.SITE_NAME,
    }

    return my_dict

Back in your settings.py, activate it by doing:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...

    # yours
    'my_context_processors.some_context_processor',
)

In your views.py, make a view use it like so:

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view(request, ...):  
    response_dict = RequestContext(request)
    ...
    # you can still still add variables that specific only to this view
    response_dict['some_var_only_in_this_view'] = 42
    ...
    return render_to_response('my_template_dir/my_template.html', response_dict)
Community
  • 1
  • 1
Geradeausanwalt
  • 2,040
  • 1
  • 17
  • 16
  • 1
    I want to use these settings in my base template from which all others inherit, and I'm a little bit lazy. Is there any way to make the default behaviour so that render_to_response will use a RequestContext instead of just a Context? I don't want to have to keep adding the same parameters throughout my whole code if I can help it. – Roger Aug 07 '10 at 14:05
  • 1
    You could write a wrapper function around render_to_response that takes request as an additional argument, and in that function: tmp_dict=RequestContext(request), tmp_dict.update(response_dict) return render_to_response('...html', tmp_dict) - this could make it easier for you change your code everywhere via search-and-replace ops – Geradeausanwalt Aug 07 '10 at 14:55
  • It's duplicated, see also http://stackoverflow.com/questions/433162/can-i-access-constants-in-settings-py-from-templates-in-django – Morty Aug 23 '14 at 09:36
5

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
3

If you only need a setting or two for a couple views, Context Processor may be overkill since it will add them to ALL views in your app. But if it's used in a lot of templates, Contest Processor is the way to go.

For the simple one off case just pass whatever setting you need from the view to the template:

from django.conf import settings
from django.shortcuts import render_to_response

def some_view(request):
    val = settings.SAVED_SETTING
    return render_to_response("index.html", {
        'saved_setting':val
    })

And access the setting in your template via:

{{ saved_setting }}
Brian Stoner
  • 158
  • 1
  • 7