Upload
loren-davie
View
967
Download
4
Embed Size (px)
DESCRIPTION
Lightning talk from django-nyc on 10/8/2008. Google project is http://code.google.com/p/django-environment
Citation preview
django-environmentenvironment variables for django
use case
repeated access of data
I wanted to be able to access a consistentper-user data set. UNIX-like variables such as
$USER, $HOME or any other arbitrary $VARIABLE.
I wanted to access them in views and in templates.
usage in application
in views
from environment import env
def hello(request): print ‘Hello accessed by’, env.avatar return render_to_response(‘hello.html’,{})
in templates# views.pyfrom environment import envdef hello(request): return render_to_response(‘hello.html’,{‘env’:env})
# hello.html<p>Hello {{ env.avatar }}</p>
shortcut# views.pyfrom environment import ctx
def hello(request): return render_to_response(‘hello.html’,ctx())
# hello.html<p>Hello {{ env.avatar }}</p>
local variables# views.pyfrom environment import lctx
def hello(request): message = ‘Howdy’ return render_to_response(‘hello.html’,lctx())
# hello.html<p>{{ message }}, {{ env.avatar }}</p>
# outputHowdy, Loren
setup
installation
• add ‘environment’ to INSTALLED_APPS
• add ‘environment.EnvironmentMiddleware’ to MIDDLEWARE_CLASSES.
• add ‘ENVPATH’ to settings.py. Should be path to directory where your .env file will live.
environment file# myapp.envfrom environment.standard import RequestParameterGenerator, AuthProfileGenerator
entries = ( ‘params’:RequestParameterGenerator(), ‘avatar’:AuthProfileGenerator(), ‘foo’:’bar’,)
generators• String
• StringDict
• URI
• Function
• ConditionalAttribute
• Cookie
• RequestAttribute
• AuthProfile
• RequestParameter
Thanks.Questions?