Live demo: django-hostmap
The routing under every link on this site
django-hostmap is the substrate for this whole demo network: it routes requests to per-package hosts from one codebase, and it is the reason every link between them just works, whether it stays on this host or jumps to another.
- Package
- django-hostmap
- This host
- hostmap.icvoss.com
- Default entry
- www
- Scheme
- https
The resolved host map
This is a live rendering of what manage.py hostmap prints on this server right now: every entry in the HOSTMAP setting, its effective host, and whether it routes, redirects, or wildcards.
Every link below is a live reverse()
No link on this page is a hard-coded URL. Each one is the output of a plain Django url template tag; hostmap decides, per call, whether the result stays relative or becomes absolute.
Compare the two "Same-host" rows above with any "Cross-host" row: same-host links, including the one to the /how/ page, stay relative because hostmap leaves them untouched; only cross-host links are rewritten absolute.
How this works, in three steps
Middleware routes the request
HostmapMiddleware matches request.get_host() against the map and sets request.urlconf. This request landed on the "hostmap" entry's URLconf because you asked for this host.
reverse() gets host-aware
A patched resolver retries other entries' URLconfs on NoReverseMatch. Same-host names resolve exactly as stock Django always has, byte-identical.
Cross-host becomes absolute
When the match lands on a different entry, the result comes back as a full URL with scheme and host, so the link works regardless of which subdomain rendered the page.
The two edge cases that matter
Named acceptance criteria for hostmap, demonstrated rather than described.
Redirect entry
The apex domain (icvoss.com, no subdomain) is a redirect entry, not a routed one. It 301s to the default entry preserving path and query string.
> 301 Moved Permanently
> Location: https://www.icvoss.com:(none)/
Wildcard subdomain
The "preview" entry matches any single subdomain level not claimed by another entry. This demo network uses it to show wildcard capture, exposed as request.hostmap.subdomain.
> request.hostmap.label == "preview"
> request.hostmap.subdomain == "pr-482"
What happens on a host nobody declared?
Not every possible subdomain is in the resolved map above. HOSTMAP_UNMATCHED decides what happens when a request arrives for a host that is not.
This site: fail open (default)
With HOSTMAP_UNMATCHED = "default" (also the setting's own default, so most deployments get this for free), an unmapped host is not an error: it falls through to the default entry (www), and the visitor lands on the main site exactly as if they had typed it directly.
> 200 OK
> served by the "www" entry, no error
Try it yourself: pick any subdomain that is not one of the demo hosts above, for example https://anything.icvoss.com/, and you will land on the main site. That is the fail-open default in action, not a fallback error page.
The alternative: reject
Setting HOSTMAP_UNMATCHED = "reject" instead makes an unmapped host a hard 404: no fallback, no guessing which entry the visitor probably meant. This site does not use it; every subdomain it does not recognise is meant to still resolve to something useful.
> 404 Not Found (only with HOSTMAP_UNMATCHED = “reject”, not this site)
Other live demos
django-hostmap (you are here)
Host-based URL routing and host-aware reversing.
django-icv-tree
Materialised path tree structures.
django-icv-taxonomy
Cross-domain taxonomy, vocabularies, and tagging.
django-icv-search
Pluggable search engine integration.
django-icv-sitemaps
Scalable sitemap and discovery file generation.
django-brickwork
A brand-agnostic UI substrate: shell, nav, components.