Login

Accessing Environment Variables in Views

Author:
LorenDavie
Posted:
July 31, 2008
Language:
Python
Version:
.96
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
19
20
21
# In this toy example we're logging information about a user profile (the 'avatar') and request parameters.

# views.py

from django.shortcuts import render_to_response
from environment import env
import logging

log = logging.getLogger('environ_test')

def test_view(request):
    log.debug('Logging request from avatar %s using params %s' % (str(env.avatar),str(env.params)))
    return render_to_response('test.html',{})

# to make this work, we would need to make the following environment file (.env)
from environment.standard import RequestParameterGenerator, AuthProfileGenerator

entries = {
    'avatar':AuthProfileGenerator(), # gets the auth profile model instance associated with the current user
    'params':RequestParameterGenerator(), # gets the request parameters as a dictionary
}

More like this

  1. Accessing Environment Variables in Templates by LorenDavie 7 years ago
  2. Rails-like environments using Django by ElfSternberg 5 years, 9 months ago
  3. showing environment variables in the django admin by tonemcd 5 years, 6 months ago
  4. Model dependency graph using graphviz (script) by andrew 6 years, 11 months ago
  5. django-noserun for testing by mjt 6 years, 4 months ago

Comments

Please login first before commenting.