I have some stuff in settings.py that I'd like to be able to access from a template, but I can't figure out how to do it. I already tried
{{CONSTANT_NAME}}
but that doesn't seem to work. Is this possible?
I have some stuff in settings.py that I'd like to be able to access from a template, but I can't figure out how to do it. I already tried
{{CONSTANT_NAME}}
but that doesn't seem to work. Is this possible?
It's certainly possible if you make it possible. You can push anything you want into the template's namespace. I don't believe the values in settings.py are automatically available to a template, however.
To use a variable in a template, you must pass it in a dictionary when rendering the template. So you can use the constant if you pass it via the dictionary given to the template.
If it's a value you'd like to have for every request & template, using a context processor is more appropriate.
Here's how:
Make a *context_processors.py* file in your app directory. Let's say I want to have the ADMIN_PREFIX_VALUE
value in every context:
from django.conf import settings # import the settings file
def admin_media(context):
# return the value you want as a dictionnary. you may add multiple values in there.
return {'ADMIN_MEDIA_URL': settings.ADMIN_MEDIA_PREFIX}
add your context processor to your settings.py file:
TEMPLATE_CONTEXT_PROCESSORS = (
"your_app.context_processors.admin_media"
)
Use RequestContext
in your view to add your context processors in your template:
from django.template import Template, context, RequestContext
from django.shortcuts import render_to_response
def my_view(request):
return render_to_response(
"index.html",
context_instance=RequestContext(request)
)
and finally, in your template:
...
<a href="{{ ADMIN_MEDIA_URL }}">path to admin media</a>
...
Django provides access to certain, frequently-used settings constants to the template such as settings.MEDIA_URL
and some of the language settings if you use django's built in generic views or pass in a context instance keyword argument in the render_to_response
shortcut function. Here's an example of each case:
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.generic.simple import direct_to_template
def my_generic_view(request, template='my_template.html'):
return direct_to_template(request, template)
def more_custom_view(request, template='my_template.html'):
return render_to_response(template, {}, context_instance=RequestContext(request))
These views will both have several frequently used settings like settings.MEDIA_URL
available to the template as {{ MEDIA_URL }}
, etc.
If you're looking for access to other constants in the settings, then simply unpack the constants you want and add them to the context dictionary you're using in your view function, like so:
from django.conf import settings
from django.shortcuts import render_to_response
def my_view_function(request, template='my_template.html'):
context = {'favorite_color': settings.FAVORITE_COLOR}
return render_to_response(template, context)
Now you can access settings.FAVORITE_COLOR
on your template as {{ favorite_color }}
.