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.5k
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
510
Software Engineering for Hackers: Bridging the Two Solitudes by Tavish Armstrong
pycon2014
0
720
Other Decks in Programming
See All in Programming
負債になりにくいCSSをデザイナとつくるには?
fsubal
9
2.3k
Amazon S3 TablesとAmazon S3 Metadataを触ってみた / 20250201-jawsug-tochigi-s3tables-s3metadata
kasacchiful
0
100
お前もAI鬼にならないか?👹Bolt & Cursor & Supabase & Vercelで人間をやめるぞ、ジョジョー!👺
taishiyade
5
3.8k
Formの複雑さに立ち向かう
bmthd
1
720
sappoRo.R #12 初心者セッション
kosugitti
0
230
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
550
SwiftUI Viewの責務分離
elmetal
PRO
0
140
Domain-Driven Transformation
hschwentner
2
1.9k
さいきょうのレイヤードアーキテクチャについて考えてみた
yahiru
3
730
Honoとフロントエンドの 型安全性について
yodaka
4
250
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
370
Conform を推す - Advocating for Conform
mizoguchicoji
3
680
Featured
See All Featured
Bash Introduction
62gerente
610
210k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
Building an army of robots
kneath
302
45k
A Philosophy of Restraint
colly
203
16k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.4k
GraphQLとの向き合い方2022年版
quramy
44
13k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
29
2.2k
Building Applications with DynamoDB
mza
93
6.2k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
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