ICVOSS DJANGO PACKAGE REGISTRY

The package index django-hostmap Turn off reversing

Turn off reversing

Documentation

Goal

Disable the resolver-acquisition patch and fall back to routing-only, either as a temporary escape hatch (a Django upgrade lands before a compatible django-hostmap release) or as a deliberate, permanent choice for a project that only wants host-based routing.

Prerequisites

Steps

1. Set HOSTMAP_PATCH_REVERSE = False

# settings.py
HOSTMAP_PATCH_REVERSE = False

With the patch off:

2. Know when this happens automatically, versus when you choose it

This is the documented remediation for two situations:

  1. A Django upgrade breaks the seam. At AppConfig.ready(), hostmap runs a self-test that reverses a probe pattern through the resolver seam. If Django's internals have moved in a way the seam cannot handle, startup fails with ImproperlyConfigured naming this setting as the fix. This is a deliberate hard failure: better a loud startup error than a subtly broken production deploy.
  2. A newer, not-yet-broken Django version. hostmap.W004 warns at startup when the running Django is newer than the package's tested ceiling, even if the seam self-test still passes. This is advance notice, not an error; you can keep running with the patch on, but if reversing starts misbehaving you already know the likely cause and the fix.

If you are choosing routing-only deliberately (not as a temporary workaround), audit any template or Python code relying on the old cross-host absolute URLs and switch those specific call sites to the explicit API:

from hostmap.urls import build_absolute_uri

build_absolute_uri("api:user-detail", args=[7])

This still gives you an absolute, cross-host URL where you need one; only the automatic behaviour of stock reverse() is gone.

Verify it worked

import pytest
from django.urls import NoReverseMatch, reverse


@pytest.mark.django_db
def test_stock_reverse_no_longer_crosses_hosts(settings):
    settings.HOSTMAP_PATCH_REVERSE = False
    with pytest.raises(NoReverseMatch):
        reverse("api:user-detail", args=[7])  # assuming this only exists on "api"


@pytest.mark.django_db
def test_routing_still_works(client, settings):
    settings.HOSTMAP_PATCH_REVERSE = False
    response = client.get("/users/7/", SERVER_NAME="api.example.com")
    assert response.status_code == 200


def test_explicit_api_still_works(settings):
    settings.HOSTMAP_PATCH_REVERSE = False
    from hostmap.urls import build_absolute_uri

    assert build_absolute_uri("api:user-detail", args=[7]).startswith("https://api.")

Note that HOSTMAP_PATCH_REVERSE is read once at AppConfig.ready() to decide whether to install the seam; flipping it via settings in a live test process does not retroactively install or remove the patch. Override it in your settings module before the app starts (or accept that a test flipping it mid-process is exercising the explicit API's independence from the patch, not a live toggle of the patch itself).

Common pitfalls