Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Django] URL prefix with runserver

[Django] URL prefix with runserver

Sébastien Fievet

February 27, 2013
Tweet

More Decks by Sébastien Fievet

Other Decks in Programming

Transcript

  1. 1

  2. “ ” At any point, your urlpatterns can “include” other

    URLconf modules. This essentially “roots” a set of URLs below other ones. -- Including other URLconfs
  3. # -- 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)), )
  4. “ ” i18n_patterns() is only allowed in your root URLconf.

    Using it within an included URLconf will throw an ImproperlyConfigured exception. -- i18_patterns limitations
  5. 2

  6. # -- myproject/myproject/settings.py ROOT_URL = '/myproject' # ... snip ...

    MIDDLEWARE_CLASSES = ( 'myproject.middleware.PrefixMiddleware', # ... snip ... )
  7. # -- 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)
  8. # -- 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
  9. # -- 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
  10. 3

  11. # -- 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')
  12. 4

  13. # -- 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)
  14. # -- 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')