Accessing Environment Variables in Views

 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
}

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.