Allow configurable subdirectory django deployment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# In your settings.py

URL_PREFIX = "/subdirectory"


# At the bottom of your urls.py

# Prefix all the above patterns with URL_PREFIX
# Note: All urls must begin with ^, and URL_PATTERN must begin with /
if settings.URL_PREFIX:
    prefixed_urlpattern = []
    for pat in urlpatterns:
        pat.regex = re.compile(r"^%s/%s" % (settings.URL_PREFIX[1:], pat.regex.pattern[1:]))
        prefixed_urlpattern.append(pat)
    urlpatterns = prefixed_urlpattern

More like this

  1. Easy configuration for relocatable sites by gsakkis 1 year, 9 months ago
  2. Keep settings.py in version control safely by mboersma 5 years, 2 months ago
  3. staticview for app by limodou 4 years, 11 months ago
  4. Serve static media files from app/media subdirectory by adamlofts 3 years, 9 months ago
  5. easy absolute path for settings.py by pgugged 3 years, 10 months ago

Comments

lallulli (on October 27, 2010):

I suggest another way, which is simpler and more pythonic IMHO.

In your urls.py rename urlpatterns to base_urlpatterns; then add the followinig definition at the end of the same file:

urlpatterns = patterns('',
    '^', include(base_urlpatterns), # iff you wish to maintain the un-prefixed URL's too
    '^your_prefix/', include(base_urlpatterns),
)

#

(Forgotten your password?)