Easy configuration for relocatable sites

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import re
from django.conf import settings


def relocate_site(root, urlpatterns, relocate_settings=('LOGIN_URL',
                                                        'LOGOUT_URL',
                                                        'LOGIN_REDIRECT_URL',
                                                        'MEDIA_URL',
                                                        'ADMIN_MEDIA_PREFIX')):
    '''Relocate a site under a different mount point.

    Typically should be used in the top level ``urls.py``::

        from django.conf import settings
        from django.conf.urls.defaults import *

        urlpatterns = patterns('',
            ...
        )

        # define URL_PREFIX in settings.py
        relocate_site(settings.URL_PREFIX, urlpatterns)

    :param root: The site's mount point, e.g. '/myapp/'.
    :params urlpatterns: The top level url patterns list.
    :param relocate_settings: The setting variables with URL values to be also
        relocated. See also http://code.djangoproject.com/ticket/8906.
    '''
    root = root.strip('/')
    if root:
        for p in urlpatterns or ():
            p.regex = re.compile(r'^%s/%s' % (re.escape(root),
                                              p.regex.pattern.lstrip('^')))
        for name in relocate_settings or ():
            url = getattr(settings, name)
            if url.startswith('/'):
                setattr(settings, name, '/' + root + url)

More like this

  1. SSL Middleware for Webfaction by parlar 6 years, 11 months ago
  2. Read settings from local_settings.py by statico 4 years, 2 months ago
  3. Middleware that fixes URLs generation for admin and {% url %} for SCGI and WSGI by dottedmag 6 years, 9 months ago
  4. nginx x-accel-redirect protection of static files by sean 6 years, 4 months ago
  5. pavement file for deploying django projects by bl4th3rsk1t3 4 years, 10 months ago

Comments

(Forgotten your password?)