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
}

More like this

  1. Accessing Environment Variables in Templates by LorenDavie 4 years, 10 months ago
  2. Dynamic Backends by LorenDavie 4 years, 8 months ago
  3. monkey-patch django to use jinja2 templates for 404/500 pages and 3rd-party apps by brondsem 4 years, 3 months ago
  4. GoogleAdmin: GMaps base layer in Geographic Admin (GeoDjango) by jbronn 4 years, 8 months ago
  5. Use django-admin.py instead of manage.py by whiteinge 5 years ago

Comments

(Forgotten your password?)