Login

Easy configuration for relocatable sites

Author:
gsakkis
Posted:
August 6, 2010
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Deploying relocatable Django sites isn't currently as trivial as it should be (see http://code.djangoproject.com/ticket/8906, http://groups.google.com/group/django-developers/tree/browse_frm/thread/fa3661888716f940/). This snippet relocates all url patterns (similarly to http://djangosnippets.org/snippets/2129/) as well as the absolute url settings of settings.py.

This allows deployment under a different mount point with a single Django setting, without having to repeat the mount point again as a SCRIPT_NAME parameter supplied by the web server.

 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.