No project description provided
Project description
django-hostroute is a high-performance host-header routing middleware for Django.
django-hostroute treats host-routing as a single, isolated Mechanism:
- Zero-Dependency: Pure Python and Django standard library. No database migrations, extra system dependencies, or thread-blocking logic.
- Instant Routing: Swaps Django
urlconfrouting paths via dictionary lookups. - Strict Isolation: Separates routing (the middleware engine) from routing (the dictionary mappings configured in
settings.py).
Installation
pip install django-hostroute
Quick Start
1. Register the Middleware
Register django_hostroute.HostRouteMiddleware at the very top of your MIDDLEWARE list in settings.py. This ensures routing is resolved before security, session, or CSRF layers execute.
# settings.py
MIDDLEWARE = [
# Must execute before Security and WhiteNoise
'django_hostroute.HostRouteMiddleware',
'django.middleware.security.SecurityMiddleware',
# ...
]
2. Configure the Routing Policy
Define your domain-to-URLconf mappings in settings.py.
# settings.py
# 1. Authorize your hosts
ALLOWED_HOSTS = [
'example.com',
'api.example.com',
'docs.example.com',
]
# 2. Add Routing
HOSTROUTE_MAP = {
'example.com': 'myproject.core.urls',
'api.example.com': 'myproject.api.urls',
'docs.example.com': 'myproject.docs.urls',
}
# 3. Define the fallback URLconf for unmapped hosts (Optional)
HOSTROUTE_DEFAULT = 'myproject.core.urls'
# 4. Define short-circuit status code used (Optional)
HOSTROUTE_DISALLOWED_STATUS = 403
Local Development & Testing
When testing locally, Nginx and Django read the HTTP Host header. You can test your local subdomain matrix easily using one of two methods:
Option A: The .localhost Standard (Zero-Config)
Modern browsers natively resolve any domain ending in .localhost to your local machine (127.0.0.1). Simply add these hosts to your ALLOWED_HOSTS and HOSTROUTE_MAP in your local settings:
HOSTROUTE_MAP = {
'api.localhost': 'myproject.api.urls',
'docs.localhost': 'myproject.docs.urls',
}
Run python manage.py runserver and navigate to http://api.localhost:8000 in your browser. The middleware automatically strips the port and routes the request.
Option B: Raw Header Injection (CI/CD / Curl)
When testing inside headless containers or CI/CD pipelines, bypass DNS altogether by connecting to 127.0.0.1 but explicitly spoofing the Host header:
curl -H "Host: api.local" [http://127.0.0.1:8000/](http://127.0.0.1:8000/)
License
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_hostroute-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_hostroute-0.1.1-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a684d63de12b36e58758c4375674b31ddb6f2ad75cec397f82a7c410c6f3a4b0
|
|
| MD5 |
faa8b7a50711ce8e226ee02e5997219d
|
|
| BLAKE2b-256 |
b5a51159fea468787eb529dc1482be368ed5728b9342685926df71c6499a8a23
|