{"id":7172,"library":"django-hosts","title":"Django Hosts","description":"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.","status":"active","version":"7.0.0","language":"en","source_language":"en","source_url":"https://github.com/jazzband/django-hosts","tags":["django","routing","hosts","subdomains","urls","middleware"],"install":[{"cmd":"pip install django-hosts","lang":"bash","label":"Install stable release"}],"dependencies":[],"imports":[{"symbol":"host","correct":"from django_hosts import host"},{"symbol":"patterns","correct":"from django_hosts import patterns"},{"note":"Prior to v6.0.0 (deprecated in v5.0.0), `reverse_host` (or `reverse`) was located in `django_hosts.utils`. It was moved to `django_hosts.resolvers`.","wrong":"from django_hosts.utils import reverse_host","symbol":"reverse","correct":"from django_hosts.resolvers import reverse"},{"symbol":"HostMiddleware","correct":"from django_hosts.middleware import HostMiddleware"}],"quickstart":{"code":"# myproject/settings.py\nINSTALLED_APPS = [\n    # ...\n    'django_hosts',\n    # ...\n]\n\nMIDDLEWARE = [\n    # ... standard Django middleware ...\n    'django_hosts.middleware.HostMiddleware',\n    # HostMiddleware must be after CommonMiddleware, SessionMiddleware, AuthenticationMiddleware\n    # and before any middleware that relies on resolved URLs/request.urlconf.\n    # ...\n]\n\nROOT_URLCONF = 'myproject.urls' # Default Django URLconf\nROOT_HOSTCONF = 'myproject.hosts' # django-hosts root hostconf\nDEFAULT_HOST = 'www' # The name of the host to use if no other matches (e.g., www.example.com)\n\n\n# myproject/hosts.py\nfrom django_hosts import patterns, host\n\nhost_patterns = patterns(\n    '',\n    # The 'www' host: maps to myproject.urls\n    host(r'www', 'myproject.urls', name='www'),\n    # A 'dashboard' subdomain host: maps to myproject.dashboard_urls\n    host(r'dashboard', 'myproject.dashboard_urls', name='dashboard'),\n    # A wildcard host for any other subdomain (e.g., user.example.com): maps to myproject.user_urls\n    host(r'(?!www|dashboard).+', 'myproject.user_urls', name='wildcard_user'),\n)\n\n\n# myproject/urls.py (for 'www' host)\nfrom django.urls import path\nfrom django.http import HttpResponse\n\ndef main_home_view(request):\n    return HttpResponse(f\"Hello from {request.host.name} (main site)!\")\n\nurlpatterns = [\n    path('', main_home_view, name='home'),\n]\n\n\n# myproject/dashboard_urls.py (for 'dashboard' host)\nfrom django.urls import path\nfrom django.http import HttpResponse\n\ndef dashboard_view(request):\n    return HttpResponse(f\"Hello from {request.host.name} (dashboard)!\")\n\nurlpatterns = [\n    path('', dashboard_view, name='home'),\n    path('settings/', lambda req: HttpResponse('Dashboard settings'), name='settings'),\n]\n\n\n# myproject/user_urls.py (for wildcard host)\nfrom django.urls import path\nfrom django.http import HttpResponse\n\ndef user_profile_view(request):\n    return HttpResponse(f\"Hello from {request.host.name} (user profile)!\")\n\nurlpatterns = [\n    path('', user_profile_view, name='profile_home'),\n]\n\n\n# Example usage in a template or view (after setting up the project)\nfrom django_hosts.resolvers import reverse\n\n# To reverse to the home page on the 'www' host:\nwww_home_url = reverse('home', host='www') # / (on www.example.com)\n\n# To reverse to the settings page on the 'dashboard' host:\ndashboard_settings_url = reverse('settings', host='dashboard') # /settings/ (on dashboard.example.com)\n\n# To reverse to a URL on a wildcard host (e.g., 'john.example.com'):\njohn_profile_url = reverse('profile_home', host='john') # / (on john.example.com)\n\nprint(f\"WWW Home URL: {www_home_url}\")\nprint(f\"Dashboard Settings URL: {dashboard_settings_url}\")\nprint(f\"John's Profile URL: {john_profile_url}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your project runs on Django 3.2+ and Python 3.9+ before upgrading to django-hosts 7.x. For older Django/Python, use an earlier django-hosts version (e.g., 6.x for Django 3.1+/Python 3.7+).","message":"Django Hosts v7.0.0 drops support for Django versions older than 3.2 and Python versions older than 3.9.","severity":"breaking","affected_versions":"7.0.0+"},{"fix":"Update your imports from `from django_hosts.utils import reverse_host` to `from django_hosts.resolvers import reverse` (or `resolve`). The function was also renamed to simply `reverse` for consistency with Django's URL resolver.","message":"The `reverse_host` and `resolve_host` functions were moved from `django_hosts.utils` to `django_hosts.resolvers` in v5.0.0 and removed from `utils` in v6.0.0.","severity":"breaking","affected_versions":"6.0.0+"},{"fix":"Ensure `django_hosts.middleware.HostMiddleware` is placed *after* standard Django middleware like `CommonMiddleware`, `SessionMiddleware`, and `AuthenticationMiddleware`, but *before* any middleware that relies on the resolved URL or `request.urlconf`.","message":"Incorrect placement of `HostMiddleware` in `MIDDLEWARE` can lead to `HostNotFound` errors or unexpected routing behavior.","severity":"gotcha","affected_versions":"All"},{"fix":"Always use `from django_hosts.resolvers import reverse` when generating URLs for host-aware routes defined with `django-hosts`.","message":"Using Django's built-in `django.urls.reverse` instead of `django_hosts.resolvers.reverse` will not respect host patterns and can lead to `NoReverseMatch` errors or incorrect URLs.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change the import statement to `from django_hosts.resolvers import reverse` and use `reverse` directly.","cause":"The `reverse_host` function (or `reverse`) was moved from `django_hosts.utils` to `django_hosts.resolvers` in django-hosts v5.0.0 and removed from the old location in v6.0.0.","error":"ModuleNotFoundError: No module named 'django_hosts.utils.reverse_host'"},{"fix":"Replace `django.urls.reverse` with `django_hosts.resolvers.reverse`. Remember to specify the `host` argument (e.g., `reverse('home', host='www')`).","cause":"This usually happens when you are trying to reverse a URL that is defined within a host-specific URLconf, but you are using Django's default `django.urls.reverse` instead of `django_hosts.resolvers.reverse`.","error":"django.urls.exceptions.NoReverseMatch: Reverse for '...' with arguments '()' and keyword arguments '{}' not found."},{"fix":"Add `'django_hosts.middleware.HostMiddleware'` to your `MIDDLEWARE` list in `settings.py`. Ensure it's placed after standard Django middleware (e.g., `CommonMiddleware`).","cause":"Django Hosts requires its middleware to be active to correctly determine the current host and set `request.host` and `request.urlconf`.","error":"ImproperlyConfigured: The HostMiddleware must be installed."}]}