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)