Login

Accessing Environment Variables in Templates

Author:
LorenDavie
Posted:
July 31, 2008
Language:
HTML/template
Version:
Not specified
Tags:
django-environment
Score:
0 (after 0 ratings)

This snippet uses the django-environment project. Django-environment is used to provide "environment variables" to django apps.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# In this example we're accessing an auth profile (avatar), and a static string in a template.

# First we would need the following environment file (.env)
from environment.standard import AuthProfileGenerator
entries = {
    'avatar':AuthProfileGenerator(),
    'greeting':'Hello',
}

# views.py
from environment import ctx
from django.shortcuts import render_to_response

def hello(request):
    return render_to_response('hello.html',ctx())

# hello.html
<p>{{ env.greeting }}, {{ env.avatar }}</p>

More like this

Comments

Please login first before commenting.