Django Hosts

7.0.0 · active · verified Thu Apr 16

Django Hosts is a library for Django that provides dynamic and static host resolving, allowing you to map different hostnames (e.g., subdomains) to distinct URL configurations (URLconfs). It's actively maintained by Jazzband, with version 7.0.0 requiring Python 3.9+ and Django 3.2+, and typically sees updates following major Django releases.

Common errors

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →