Login

Accessing Environment Variables in Views

Author:
LorenDavie
Posted:
July 31, 2008
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.