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)