- Author:
- EmilStenstrom
- Posted:
- July 30, 2010
- Language:
- Python
- Version:
- Not specified
- Score:
- 2 (after 2 ratings)
I wanted a way to deploy a Django site to both the root of a domain, and to a subdirectory. The solution was to loop over all urlpatterns and add a configurable string (URL_PREFIX) at the beginning of all patterns.
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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
I suggest another way, which is simpler and more pythonic IMHO.
In your
urls.py
renameurlpatterns
tobase_urlpatterns
; then add the followinig definition at the end of the same file:#
@lallulli: I agree, your solution is much better!
#
Please login first before commenting.