django-hosts
Dynamic and static host resolving for Django. Maps hostnames to URLconfs.
What it is and what it does
django-hosts is a Django middleware package that intercepts incoming HTTP requests and routes them to different URL configurations based on the hostname. Instead of handling multiple subdomains within a single URLconf, you define separate hostconfs—modules containing URL patterns—and map hostnames (or patterns matching hostnames) to them. Requests to api.example.com route to one URLconf, beta.example.com to another, and unmatched hosts fall back to the default ROOT_URLCONF.
The package uses regular expressions to match hostnames against patterns, evaluated in order. It requires minimal configuration: add two middleware classes to your MIDDLEWARE setting, create a hosts.py file with your host patterns, and set ROOT_HOSTCONF and DEFAULT_HOST in settings. No runtime dependencies means a lightweight addition to any Django project.
Use it for:
- Serve an API on api.example.com with its own URL structure separate from the main site.
- Host a beta version at beta.example.com with different views and routes than production.
- Route multiple branded subdomains to different content or applications within one Django project.
- Implement multi-tenant deployments where each tenant's subdomain maps to its own URL configuration.
- Separate admin interfaces, documentation, or internal tools onto dedicated subdomains.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Routes HTTP requests to different Django URL configurations based on the requested hostname, enabling a single Django project to serve multiple subdomains with distinct URL schemes.
Yes. django-hosts is production-stable, actively maintained, has no external dependencies, and solves a common Django deployment pattern cleanly. Use it when you need to route multiple subdomains to different URL schemes within a single Django project—it's the standard tool for this task.
Install
django-hosts on PyPI
pip
pip install django-hostsuv
uv add django-hostspoetry
poetry add django-hostsInstalling django-hosts
Before you install
Low install friction with no runtime dependencies. Active maintenance with recent commits and a stable release cadence; supports current Python versions (3.9–3.13) and modern Django releases (4.2, 5.1, 5.2).
License in practice
BSD license is permissive, allowing commercial and private use with minimal restrictions—suitable for most projects.
Quickstart
pip install django-hosts
# In hosts.py:
from django_hosts import patterns, host
host_patterns = patterns('',
host(r'api', 'api.urls', name='api'),
host(r'beta', 'beta.urls', name='beta'),
)
# In settings.py:
INSTALLED_APPS = ['django_hosts', ...]
MIDDLEWARE = ['django_hosts.middleware.HostsRequestMiddleware', ...]
ROOT_HOSTCONF = 'mysite.hosts'
DEFAULT_HOST = 'api'
Package facts
| License | BSD (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 421 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 397,715/month — #6,956 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_hosts-7.0.0-py3-none-any.whl
Tags
More Dynamic Content packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
soupsieveSoupsieve is a CSS selector library designed to…
permissive · top 100 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
FlaskFlask is a lightweight WSGI web application…
permissive · top 1,000 on PyPI
MakoMako compiles Python-embedded templates into…
permissive · top 1,000 on PyPI
dj-staticWraps a WSGI application to serve static files…
permissive · top 15,000 on PyPI
django-constanceDjango-constance lets you store and manage…
permissive · top 5,000 on PyPI
drf-nested-routersProvides routers and serializer fields for…
permissive · top 5,000 on PyPI
django-permissions-policySets the Permissions-Policy HTTP header on…
permissive · top 15,000 on PyPI
django-cache-urlConfigures Django cache backends from…
permissive · top 15,000 on PyPI
django-cspDjango-CSP adds Content-Security-Policy headers…
permissive · top 5,000 on PyPI
tldparseParses domain names into their constituent…
permissive · top 15,000 on PyPI
django-waffleDjango Waffle provides feature flags (toggles)…
permissive · top 5,000 on PyPI
webapp2A lightweight Python web framework designed for…
permissive · top 15,000 on PyPI
repathConverts Express-style path strings (like…
permissive · top 15,000 on PyPI