views:

4608

answers:

4

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?

+1  A: 

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.

Brian Clapper
+3  A: 

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.

mipadi
+42  A: 

If it's a value you'd like to have for every request & template, using a context processor is more appropriate.

Here's how:

  1. 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}
    
  2. add your context processor to your settings.py file:

    TEMPLATE_CONTEXT_PROCESSORS = (
    "your_app.context_processors.admin_media"
    

    )

  3. 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)
        )
    
  4. and finally, in your template:

    ...
    <a href="{{ ADMIN_MEDIA_URL }}">path to admin media</a>
    ...
    
bchhun
Nice example. One suggestion, the admin_media function should probably take 'request' as the argument not 'context' to make it clear that custom context processors need to accept a HttpRequest object.
mcqwerty
+15  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 }}.

Prairiedogg
It's worth noting that the specific values added by using a RequestContext is dependent on the value of TEMPLATE_CONTEXT_PROCESSORS. Thus if you want additional values passed in everywhere, just write your own context processor and add it to TEMPLATE_CONTEXT_PROCESSORS.
Carl Meyer
A point on consistency, in the generic views, and many of the core and contrib apps, additional context is called extra_context, and very often it is included in the view's arguments.
Soviut