Install & Compatibility
Where this runs
tested against v7.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.2MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.5s · import 0.000s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
host
✓ from django_hosts import host
✗ from django_hosts import host
patterns
✓ from django_hosts import patterns
reverse
✓ from django_hosts import reverse
This quickstart demonstrates how to configure `django-hosts` for a Django project with multiple subdomains. It involves setting `INSTALLED_APPS`, `MIDDLEWARE`, `ROOT_HOSTCONF`, and `DEFAULT_HOST` in `settings.py`, defining host patterns in a `hosts.py` file, and creating separate URLconfs for different hosts. It also shows how to use `django_hosts.resolvers.reverse` to generate host-aware URLs.
# myproject/settings.py
INSTALLED_APPS = [
# ...
'django_hosts',
# ...
]
MIDDLEWARE = [
# ... standard Django middleware ...
'django_hosts.middleware.HostMiddleware',
# HostMiddleware must be after CommonMiddleware, SessionMiddleware, AuthenticationMiddleware
# and before any middleware that relies on resolved URLs/request.urlconf.
# ...
]
ROOT_URLCONF = 'myproject.urls' # Default Django URLconf
ROOT_HOSTCONF = 'myproject.hosts' # django-hosts root hostconf
DEFAULT_HOST = 'www' # The name of the host to use if no other matches (e.g., www.example.com)
# myproject/hosts.py
from django_hosts import patterns, host
host_patterns = patterns(
'',
# The 'www' host: maps to myproject.urls
host(r'www', 'myproject.urls', name='www'),
# A 'dashboard' subdomain host: maps to myproject.dashboard_urls
host(r'dashboard', 'myproject.dashboard_urls', name='dashboard'),
# A wildcard host for any other subdomain (e.g., user.example.com): maps to myproject.user_urls
host(r'(?!www|dashboard).+', 'myproject.user_urls', name='wildcard_user'),
)
# myproject/urls.py (for 'www' host)
from django.urls import path
from django.http import HttpResponse
def main_home_view(request):
return HttpResponse(f"Hello from {request.host.name} (main site)!")
urlpatterns = [
path('', main_home_view, name='home'),
]
# myproject/dashboard_urls.py (for 'dashboard' host)
from django.urls import path
from django.http import HttpResponse
def dashboard_view(request):
return HttpResponse(f"Hello from {request.host.name} (dashboard)!")
urlpatterns = [
path('', dashboard_view, name='home'),
path('settings/', lambda req: HttpResponse('Dashboard settings'), name='settings'),
]
# myproject/user_urls.py (for wildcard host)
from django.urls import path
from django.http import HttpResponse
def user_profile_view(request):
return HttpResponse(f"Hello from {request.host.name} (user profile)!")
urlpatterns = [
path('', user_profile_view, name='profile_home'),
]
# Example usage in a template or view (after setting up the project)
from django_hosts.resolvers import reverse
# To reverse to the home page on the 'www' host:
www_home_url = reverse('home', host='www') # / (on www.example.com)
# To reverse to the settings page on the 'dashboard' host:
dashboard_settings_url = reverse('settings', host='dashboard') # /settings/ (on dashboard.example.com)
# To reverse to a URL on a wildcard host (e.g., 'john.example.com'):
john_profile_url = reverse('profile_home', host='john') # / (on john.example.com)
print(f"WWW Home URL: {www_home_url}")
print(f"Dashboard Settings URL: {dashboard_settings_url}")
print(f"John's Profile URL: {john_profile_url}")
Upgrade
Version history
7.0.0latest on PyPI · released Jun 19, 2025
Audit
Dependencies
No dependency data recorded yet.