3. Installing libraries
• Always work in a virtualenv
$ pip freeze
# ONE GAZILLION LIBRARIES!!!!
(inuittest)$ pip freeze
argparse==1.2.1
wsgiref==0.1.2
(inuittest)$ pip install yolk django south
(inuittest)$ yolk –l
Django - 1.4.2 - active
Python - 2.7.3 - active development
(/usr/lib/python2.7/lib-dynload)
South - 0.7.6 - active
argparse - 1.2.1 - active development (/usr/lib/python2.7)
pip - 1.2.1 - active
setuptools - 0.6c11 - active
wsgiref - 0.1.2 - active development (/usr/lib/python2.7)
yolk - 0.4.3 - active
7. Architecture
RDBMS
Storage ORM Fixtures
Model
Signals
Forms
Template
URL Resolver View Template Loader
Middleware Middleware
Request Response
8. Model
RDBMS
Storage ORM Fixtures
Model
Signals
Forms
Template
URL Resolver View Template Loader
Middleware Middleware
Request Response
9. Model
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
10. Model
BEGIN;
CREATE TABLE "polls_poll" (
"id" serial NOT NULL PRIMARY KEY,
"question" varchar(200) NOT NULL,
"pub_date" timestamp with time zone NOT NULL
);
CREATE TABLE "polls_choice" (
"id" serial NOT NULL PRIMARY KEY,
"poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY
DEFERRED,
"choice" varchar(200) NOT NULL,
"votes" integer NOT NULL
);
COMMIT;
12. Request Response Cycle
1. Determine which URLConf to use
2. Load that URLConf and look for
urlpatterns variable
3. Run through each pattern in order and stop
at the one that matches
4. Once a match is found, import and call the
view with the HttpRequest object
5. If no regex matches or an exception is raised,
invoke error-handling
32. Application Server (AS)
Web Server Gateway Interface (WSGI)
• PEP 333 [P. J. Eby, dec 2010]
• Common ground for portable web applications
– Routing, environment variables, load balancing,
forwarding, post-prosessing
• 2 parts:
– Gateway (= server)
– Framework (= application)
33. AS: gunicorn
• “green unicorn” (cfr. green threading)
• Pre-fork worker model
• 4-12 workers = 1000’s req/sec
• Each worker runs an instance of your django
project => memory
34. AS: uWSGI
• Protocol “uwsgi” (lowercase)
• Included in Nginx & Cherokee
• Faster than gunicorn
36. Tools
• IPython
– Tab completion in the interpreter!
• import ipdb; ipdb.set_trace()
– Just like pdb, but with added magic
• Fabric (fabfile.org)
– $ fab production deploy
– Like Capistrano but without the voodoo
• Supervisord (supervisord.org)
– Process monitoring & controlling
39. Cache Systems
CACHE_BACKEND = ....
– Dummy caching
• ‘dummy:///’
• For development, dummy!
– Local-Memory Caching
• ‘locmem:///’
• In memory caching without memcache
– Database Caching
• ‘db://my_cache_table’
• When DB I/O is not a problem.
– Filesystem Caching
• ‘file:///var/tmp/django_cache’ # directory
• Cache values stored in seperate files in pickled format
– Memcache
• ‘memcached:///127.0.0.1:112211/’
40. Approaches to Caching
• Per site caching (generic approach)
– Only cache everything without GET or POST parameters
– Only cache anonymous requests
• Per view caching (granular approach)
– Cache response of a view
– Set cache timeouts per view
• Template Fragment caching (micro manage approach)
– Caching both static and dynamic fragments
• Low level caching (micro micro manage)
– Indivudually store parameters in the cache
42. Approaches to Caching
• Per site caching (generic approach)
– Only cache everything without GET or POST parameters
– Only cache anonymous requests
• Per view caching (granular approach)
– Cache response of a view
– Set cache timeouts per view
• Template Fragment caching (micro manage approach)
– Caching both static and dynamic fragments
• Low level caching (micro micro manage)
– Indivudually store parameters in the cache
44. Approaches to Caching
• Per site caching (generic approach)
– Only cache everything without GET or POST parameters
– Only cache anonymous requests
• Per view caching (granular approach)
– Cache response of a view
– Set cache timeouts per view
• Template Fragment caching (micro manage approach)
– Caching both static and dynamic fragments
• Low level caching (micro micro manage)
– Indivudually store parameters in the cache
45. Approaches to Caching
• Per site caching (generic approach)
– Only cache everything without GET or POST parameters
– Only cache anonymous requests
• Per view caching (granular approach)
– Cache response of a view
– Set cache timeouts per view
• Template Fragment caching (micro manage approach)
– Caching both static and dynamic fragments
• Low level caching (micro micro manage)
– Indivudually store parameters in the cache
46. {% load cache %}
{% cache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endcache %}