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

  1. Accessing Environment Variables in Views by LorenDavie 6 years, 8 months ago
  2. showing environment variables in the django admin by tonemcd 5 years, 2 months ago
  3. Rails-like environments using Django by ElfSternberg 5 years, 5 months ago
  4. Simple template tag to do |stringformat filter with format from a variable by leopd 3 years, 11 months ago
  5. monkey-patch django to use jinja2 templates for 404/500 pages and 3rd-party apps by brondsem 6 years ago

Comments

Please login first before commenting.