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.5k
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.2k
"My big gay adventure. Making, releasing and selling an indie game made in python." by Luke Miller
pycon2014
2
1.5k
Farewell and Welcome Home, Python in Two Genders by Naomi_Ceder
pycon2014
1
700
Deliver Your Software in an Envelope by Augie Fackler and Nathaniel Manista
pycon2014
1
520
Hitchhikers Guide to Free and Open Source Participation by Elena Williams
pycon2014
6
1.1k
Localization Revisted (aka. Translations Evolved) by Ruchi Varshney
pycon2014
0
670
Smart Dumpster by Bradley E. Angell
pycon2014
0
480
Software Engineering for Hackers: Bridging the Two Solitudes by Tavish Armstrong
pycon2014
0
700
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
200
Amebaチョイス立ち上げの裏側 ~依存システムとの闘い~
daichi_igarashi
0
220
初めてのiOS関連GitHub ActionsをMarketplaceに公開するまでの実録
konifar
3
210
Architecture Decision Record (ADR)
nearme_tech
PRO
1
620
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
180
The Shape of a Service Object
inem
0
360
現代のVueとTypeScript - 型安全の活用術
minako__ph
4
3.2k
Ruby Parser progress report 2024
yui_knk
2
200
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
930
Mastering AsyncSequence - 使う・作る・他のデザインパターン(クロージャ、Delegate など)から移行する
treastrain
4
1.5k
どうしてこうなった?から理解するActive Recordの関連の裏側
willnet
5
540
BasicBasic認証
sadnessojisan
5
3.2k
Featured
See All Featured
A designer walks into a library…
pauljervisheath
201
24k
Unsuck your backbone
ammeep
667
57k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
25
1.3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
103
48k
How to train your dragon (web standard)
notwaldorf
85
5.6k
In The Pink: A Labor of Love
frogandcode
139
22k
Atom: Resistance is Futile
akmur
261
25k
No one is an island. Learnings from fostering a developers community.
thoeni
18
2.9k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
230
17k
From Idea to $5000 a Month in 5 Months
shpigford
379
46k
Robots, Beer and Maslow
schacon
PRO
157
8.1k
Agile that works and the tools we love
rasmusluckow
327
20k
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