Django Environment

1,230 views

Published on

Lightning talk from django-nyc on 10/8/2008. Google project is http://code.google.com/p/django-environment

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,230
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
44
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Django Environment

  1. 1. django-environment environment variables for django
  2. 2. use case
  3. 3. repeated access of data I wanted to be able to access a consistent per-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.
  4. 4. usage in application
  5. 5. in views from environment import env def hello(request): print ‘Hello accessed by’, env.avatar return render_to_response(‘hello.html’,{})
  6. 6. in templates # views.py from environment import env def hello(request): return render_to_response(‘hello.html’,{‘env’:env}) # hello.html <p>Hello {{ env.avatar }}</p>
  7. 7. shortcut # views.py from environment import ctx def hello(request): return render_to_response(‘hello.html’,ctx()) # hello.html <p>Hello {{ env.avatar }}</p>
  8. 8. local variables # views.py from environment import lctx def hello(request): message = ‘Howdy’ return render_to_response(‘hello.html’,lctx()) # hello.html <p>{{ message }}, {{ env.avatar }}</p> # output Howdy, Loren
  9. 9. setup
  10. 10. 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.
  11. 11. environment file # myapp.env from environment.standard import RequestParameterGenerator, AuthProfileGenerator entries = ( ‘params’:RequestParameterGenerator(), ‘avatar’:AuthProfileGenerator(), ‘foo’:’bar’, )
  12. 12. generators • String • RequestAttribute • StringDict • AuthProfile • URI • RequestParameter • Function • ConditionalAttribute • Cookie
  13. 13. Thanks. Questions?

×