Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
[Django] URL prefix with runserver
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Sébastien Fievet
February 27, 2013
Programming
1.5k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
[Django] URL prefix with runserver
Sébastien Fievet
February 27, 2013
More Decks by Sébastien Fievet
See All by Sébastien Fievet
Introduction to Flask
zyegfryed
0
77
Django Round-Up – Meetup Django CH #28
zyegfryed
0
69
Django Round-Up – Meetup Django CH #25
zyegfryed
0
61
Django Round-Up – Meetup Django CH #23
zyegfryed
0
59
Django Round-Up – Meetup Django CH #21
zyegfryed
0
79
Django Round-Up – Meetup Django CH #20
zyegfryed
0
84
Django Round-Up – Meetup Django CH #19
zyegfryed
2
93
[Django] Generating PDF with PDFForm
zyegfryed
0
97
[Django] RESTful API
zyegfryed
1
230
Other Decks in Programming
See All in Programming
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.1k
The Rails Doctrine Decade
koic
2
570
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
250
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
0
1.1k
AWSに止められる覚悟してますか?
morizo_1984
2
530
Workers Cache を知る
syumai
0
350
最近”作らせてる” ローカルAIアプリの話
akit37
0
450
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
気持ちぃ〜角丸 Squircle
ryu1013
0
290
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
150
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
130
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
210
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
730
Typedesign – Prime Four
hannesfritz
42
3.2k
Exploring anti-patterns in Rails
aemeredith
4
530
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
480
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Odyssey Design
rkendrick25
PRO
2
870
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Technical Leadership for Architectural Decision Making
baasie
3
600
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
So, you think you're a good person
axbom
PRO
2
2.2k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
540
Transcript
URL prefix with runserver Sébastien Fievet Meetup Django Suisse #12
February 27, 2013
What we have
What we have ๏On production http://example.com/myproject/
What we have ๏On production http://example.com/myproject/ ๏With runserver http://127.0.0.1:8000/
What we want ๏On production http://example.com/myproject/ ๏With runserver http://127.0.0.1:8000/myproject/
1
“ ” At any point, your urlpatterns can “include” other
URLconf modules. This essentially “roots” a set of URLs below other ones. -- Including other URLconfs
The trick You can also “include” urlpatterns!
# -- myproject/myproject/urls.py from django.conf import settings from django.conf.urls import
patterns, url, include # Your regular urls urlpatterns = patterns('', # ... snip ... ) # The ugly patch if settings.URL_DEBUG: urlpatterns = patterns('', url(r'^myproject/', include(urlpatterns)), )
“ ” i18n_patterns() is only allowed in your root URLconf.
Using it within an included URLconf will throw an ImproperlyConfigured exception. -- i18_patterns limitations
2
Django Middleware
# -- myproject/myproject/settings.py ROOT_URL = '/myproject' # ... snip ...
MIDDLEWARE_CLASSES = ( 'myproject.middleware.PrefixMiddleware', # ... snip ... )
# -- myproject/myproject/middleware.py import re from django.conf import settings class
PrefixMiddleware(object): def __init__(self): prefix = getattr(settings, 'ROOT_URL', '/') self.prefix = prefix.rstrip('/') self.regprefix = re.compile("^%s(.*)$" % self.prefix)
# -- myproject/myproject/middleware.py class PrefixMiddleware(object): # ... snip ... def
process_request(self, request): environ = request.environ url = environ['PATH_INFO'] url = re.sub(self.regprefix, r'\1', url) if not url: url = '/' environ['PATH_INFO'] = url environ['SCRIPT_NAME'] = self.prefix self._patch_request(request, environ) return None
# -- myproject/myproject/middleware.py class PrefixMiddleware(object): # ... snip ... def
_patch_request(self, request, environ): script_name = environ['SCRIPT_NAME'] path_info = environ['PATH_INFO'] request.environ = environ request.path_info = path_info request.path = '%s%s' % (script_name, path_info) request.META = environ request.META['PATH_INFO'] = path_info request.META['SCRIPT_NAME'] = script_name
But...
But... Monkey-patching approach
But... Monkey-patching approach Messing with Django internals is bad!
3
WSGI Middleware
pip install PasteDeploy==1.5.0 PasteDeploy
# -- myproject/myproject/wsgi.py import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.wsgi import
get_wsgi_application application = get_wsgi_application() # Apply WSGI middleware here. from paste.deploy.config import PrefixMiddleware application = PrefixMiddleware(application, prefix='/myproject')
4
WSGI Middleware (2)
WSGI Middleware (2) PasteDeploy package overkill
WSGI Middleware (2) PasteDeploy package overkill NIH syndrome!
# -- myproject/myproject/middleware.py import re class PrefixMiddleware(object): def __init__(self, app,
prefix='/'): self.app = app self.prefix = prefix.rstrip('/') self.regprefix = re.compile("^%s(.*)$" % self.prefix) def __call__(self, environ, start_response): url = environ['PATH_INFO'] url = re.sub(self.regprefix, r'\1', url) if not url: url = '/' environ['PATH_INFO'] = url environ['SCRIPT_NAME'] = self.prefix return self.app(environ, start_response)
# -- myproject/myproject/wsgi.py import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") from django.core.wsgi import
get_wsgi_application application = get_wsgi_application() # Apply WSGI middleware here. from .middleware import PrefixMiddleware application = PrefixMiddleware(application, prefix='/myproject')
Questions?