13
django-environment environment variables for django

Django Environment

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

Page 1: Django Environment

django-environmentenvironment variables for django

Page 2: Django Environment

use case

Page 3: Django Environment

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.

Page 4: Django Environment

usage in application

Page 5: Django Environment

in views

from environment import env

def hello(request): print ‘Hello accessed by’, env.avatar return render_to_response(‘hello.html’,{})

Page 6: Django Environment

in templates# views.pyfrom environment import envdef hello(request): return render_to_response(‘hello.html’,{‘env’:env})

# hello.html<p>Hello {{ env.avatar }}</p>

Page 7: Django Environment

shortcut# views.pyfrom environment import ctx

def hello(request): return render_to_response(‘hello.html’,ctx())

# hello.html<p>Hello {{ env.avatar }}</p>

Page 8: Django Environment

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

Page 9: Django Environment

setup

Page 10: Django Environment

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.

Page 11: Django Environment

environment file# myapp.envfrom environment.standard import RequestParameterGenerator, AuthProfileGenerator

entries = ( ‘params’:RequestParameterGenerator(), ‘avatar’:AuthProfileGenerator(), ‘foo’:’bar’,)

Page 12: Django Environment

generators• String

• StringDict

• URI

• Function

• ConditionalAttribute

• Cookie

• RequestAttribute

• AuthProfile

• RequestParameter

Page 13: Django Environment

Thanks.Questions?