Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A Scenic Drive through the Django Request-Respo...
Search
PyCon 2014
April 12, 2014
Programming
7
1.6k
A Scenic Drive through the Django Request-Response Cycle by Dan Langer
PyCon 2014
April 12, 2014
Tweet
Share
More Decks by PyCon 2014
See All by PyCon 2014
Postgres Performance for Humans by Craig Kerstiens
pycon2014
29
3.6k
Technical Onboarding, Training, and Mentoring by Kate Heddleston and Nicole Zuckerman
pycon2014
1
2.3k
"My big gay adventure. Making, releasing and selling an indie game made in python." by Luke Miller
pycon2014
2
1.6k
Farewell and Welcome Home, Python in Two Genders by Naomi_Ceder
pycon2014
1
720
Deliver Your Software in an Envelope by Augie Fackler and Nathaniel Manista
pycon2014
1
530
Hitchhikers Guide to Free and Open Source Participation by Elena Williams
pycon2014
6
1.2k
Localization Revisted (aka. Translations Evolved) by Ruchi Varshney
pycon2014
0
690
Smart Dumpster by Bradley E. Angell
pycon2014
0
520
Software Engineering for Hackers: Bridging the Two Solitudes by Tavish Armstrong
pycon2014
0
720
Other Decks in Programming
See All in Programming
Domain-Driven Design (Tutorial)
hschwentner
13
22k
Serverless Rust: Your Low-Risk Entry Point to Rust in Production (and the benefits are huge)
lmammino
1
150
Rails アプリ地図考 Flush Cut
makicamel
1
130
もう少しテストを書きたいんじゃ〜 #phpstudy
o0h
PRO
17
4k
もう僕は OpenAPI を書きたくない
sgash708
6
1.9k
Datadog DBMでなにができる? JDDUG Meetup#7
nealle
0
150
コードを読んで理解するko build
bells17
1
110
未経験でSRE、はじめました! 組織を支える役割と軌跡
curekoshimizu
1
160
SwiftUI Viewの責務分離
elmetal
PRO
2
270
ファインディLT_ポケモン対戦の定量的分析
fufufukakaka
0
920
TCAを用いたAmebaのリアーキテクチャ
dazy
0
130
sappoRo.R #12 初心者セッション
kosugitti
0
280
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Docker and Python
trallard
44
3.3k
Building an army of robots
kneath
303
45k
A designer walks into a library…
pauljervisheath
205
24k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Unsuck your backbone
ammeep
669
57k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
52k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
How GitHub (no longer) Works
holman
314
140k
A Tale of Four Properties
chriscoyier
158
23k
Transcript
THE DJANGO REQUEST-RESPONSE CYCLE A Scenic Drive Though Daniel Langer
[email protected]
@dlanger
DANIEL LANGER Je m’appelle Handmade in Toronto
THE DJANGO REQUEST-RESPONSE CYCLE A Scenic Drive Though
LOTS OF THESE Django has http://www.safetysign.com/images/catlog/product/large/J6014.png
CLIENT
THE REQUEST
THE REQUEST $ curl -v http://www.daniellanger.com/hello/mtl/
THE REQUEST $ curl -v http://www.daniellanger.com/hello/mtl/ > GET /hello/mtl/ HTTP/1.1
> Host: www.daniellanger.com > Accept: */*
http://www.google.com/about/datacenters/gallery/#/all/5 THE INTERNET http://www.google.com/about/datacenters/gallery/#/all/5
THE WEB SERVER http://bit.ly/1jPAhEC
INTERFACE? mod_python FastCGI CGI
WEB SERVER GATEWAY INTERFACE PEP 333 December 2007 Phillip Eby
WEB SERVER GATEWAY INTERFACE def simple_app(environ, start_response): response_headers = [('Content-type',
'text/plain')] start_response(‘200 OK’, response_headers) return ['Hello world!\n']
DJANGO SPEAKS WSGI class WSGIHandler(base.BaseHandler): def __call__(self, environ, start_response): [...]
response = self.get_response(request) [...] start_response(status, response_headers) return response
DJANGO.CORE.HANDLERS.BASEHANDLER 1. Request middleware 2. Resolve request.path_info 3. View middleware
4. View 5. Response middleware * Exception middleware (maybe)
MIDDLEWARE? A small digression
MIDDLEWARE A small digression “[A] light, low-level ‘plugin’ system for
globally altering Django’s input or output.” - https://docs.djangoproject.com/en/dev/topics/http/middleware/
MIDDLEWARE A small digression MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', [...]
)
DJANGO.CORE.HANDLERS.BASEHANDLER 1. Request middleware 2. Resolve request.path_info 3. View middleware
4. View 5. Response middleware * Exception middleware (maybe)
REQUEST MIDDLEWARE class PyConRequestMiddleware(object): def process_request(self, request): return None #
or HttpResponse
BREAK TIME http://bit.ly/1mNlxWE
CLIENT WEB SERVER WSGI REQUEST Middleware
DJANGO.CORE.HANDLERS.BASEHANDLER 1. Request middleware 2. Resolve request.path_info 3. View middleware
4. View 5. Response middleware * Exception middleware (maybe)
DJANGO.CORE.URLRESOLVERS http://www.daniellanger.com/hello/mtl/
DJANGO.CORE.URLRESOLVERS http://www.daniellanger.com/hello/mtl/ urlpatterns = patterns('' url(r'^hello/mtl/$', HiMtlView, name=‘hi__mtl’), url(r'^hello/(?P<city>\w+)/$', HiCityView,
name=‘hi_city’), )
DJANGO.CORE.URLRESOLVERS http://www.daniellanger.com/hello/mtl/ urlpatterns = patterns('' url(r'^hello/(?P<city>\w+)/$', HiCityView, name=‘hi_city’), url(r'^hello/mtl/$', HiMtlView,
name=‘hi__mtl’), ]
DJANGO.CORE.URLRESOLVERS urlpatterns = patterns('' url(r'^hello/(?P<city>\w+)/$', HiCityView, name=‘hi_city’), ) {% url
‘hi_city’ city=ottawa %} reverse(‘hi_city’, kwargs={‘city’: ‘ottawa’}) # Both return http://www.daniellanger.com/hello/ottawa/
DJANGO.CORE.URLRESOLVERS urlpatterns = patterns('' url(r'^cdn_hi/(?P<city>\w+)/$', HiCityView, name=‘hi_city’), ) {% url
‘hi_city’ city=ottawa %} reverse(‘hi_city’, kwargs={‘city’: ‘ottawa’}) # Both return http://www.daniellanger.com/cdn_hi/ottawa/
DJANGO.CORE.URLRESOLVERS http://www.daniellanger.com/hello/mtl/ urlpatterns = patterns('' url(r'^hello/mtl/$', HiMtlView, name=‘hi__mtl’), url(r'^hello/(?P<city>\w+)/$', HiCityView,
name=‘hi_city’), )
DJANGO.CORE.HANDLERS.BASEHANDLER 1. Request middleware 2. Resolve request.path_info 3. View middleware
4. View 5. Response middleware * Exception middleware (maybe)
DJANGO.CORE.HANDLERS.BASEHANDLER 1. Request middleware 2. Resolve request.path_info 3. View middleware
4. View 5. Response middleware * Exception middleware (maybe)
VIEWS def HiMtlView(request): html = “Bonjour <b>Mtl</b>!” return HttpResponse(html)
RESPONSES? HttpResponse HttpResponseRedirect JsonResponse StreamingHttpResponse
VIEWS def HiMtlView(request): html = “Bonjour <b>Mtl</b>!” return HttpResponse(html)
CLASS-BASED VIEWS from django.views.generic import View class HiMtlView(View): def get(self,
request): html = “Bonjour <b>Mtl</b>!” return HttpResponse(html)
TEMPLATES IN VIEWS from django.shortcuts import render def HiMtlView(request): context
= {‘city’: ‘Montreal’} return render(request, ‘hello.html’, context) # hello.html Bonjour <b>{{ city }}</b>!
TEMPLATES IN VIEWS def HiMtlView(request): context = {‘city’: ‘Montreal’} return
render(request, ‘hello.html’, context) # hello.html Bonjour <b>{{ city }}</b> (from {{ request_city }}!)
TEMPLATES IN VIEWS def HiMtlView(request): context = { ‘city’: ‘Montreal’,
‘request_city’: GeoIP.get_city(request)} return render(request, ‘hello.html’, context) # hello.html Bonjour <b>{{ city }}</b> (from {{ request_city }}!)
CONTEXT PROCESSORS def HiMtlView(request): context = { ‘city’: ‘Montreal’, ‘request_city’:
GeoIP.get_city(request)} return render(request, ‘hello.html’, context) # hello.html Bonjour <b>{{ city }}</b> (from {{ request_city }}!)
CONTEXT PROCESSORS def request_city(request) return { ‘request_city’: GeoIP.get_city(request), }
CONTEXT PROCESSORS def HiMtlView(request): context = {‘city’: ‘Montreal’} return render(request,
‘hello.html’, context) # hello.html Bonjour <b>{{ city }}</b> (from {{ request_city }}!)
The Doe Corporation presents PAUSE PAUSE- CAFE http://www.roadfood.com/photos/20865.jpg
CLIENT WEB SERVER WSGI URL RESOLUTION VIEW REQUEST Middleware VIEW
Middleware
DJANGO.CORE.HANDLERS.BASEHANDLER 1. Request middleware 2. Resolve request.path_info 3. View middleware
4. View 5. Response middleware * Exception middleware (maybe)
RESPONSE MIDDLEWARE class PyConResponseMiddleware(object): def process_response(self, request, response): return None
# or HttpResponse
MIDDLEWARE class PyConMiddleware(object): def process_request(self, request): return None # or
HttpResponse def process_response(self, request, response): return response # or new HttpResponse
MIDDLEWARE MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'PyConMiddleware', [...] )
DJANGO.CORE.HANDLERS.BASEHANDLER 1. Request middleware 2. Resolve request.path_info 3. View middleware
4. View 5. Response middleware * Exception middleware (maybe)
EXCEPTION MIDDLEWARE class PyConMiddleware(object): def process_exception(self, request, exception): return None
# or HttpResponse
WEB SERVER GATEWAY INTERFACE class WSGIHandler(base.BaseHandler): def __call__(self, environ, start_response):
[...] response = self.get_response(request) [...] start_response(status, response_headers) return response
THE RESPONSE $ curl -v http://www.daniellanger.com/hello/mtl/ > GET /hello/mtl/ HTTP/1.1
> Host: www.daniellanger.com > Accept: */* < HTTP/1.1 200 OK < Server: nginx < Content-Type: text/html;charset=UTF-8 < Content-Length: 33 Bonjour Montreal! (from Toronto)
CLIENT WEB SERVER WSGI URL RESOLUTION VIEW WSGI WEB SERVER
CLIENT REQUEST Middleware RESPONSE Middleware VIEW Middleware EXCEPTION Middleware
@dlanger