Skip to main content
https://img.shields.io/pypi/v/django-hosts.svg https://img.shields.io/pypi/pyversions/django-hosts.svg https://img.shields.io/pypi/djversions/django-hosts.svg https://github.com/jazzband/django-hosts/workflows/Test/badge.svg https://codecov.io/gh/jazzband/django-hosts/branch/master/graph/badge.svg https://readthedocs.org/projects/django-hosts/badge/?version=latest&style=flat https://jazzband.co/static/img/badge.svg

This Django app routes requests for specific hosts to different URL schemes defined in modules called “hostconfs”.

For example, if you own example.com but want to serve specific content at api.example.com and beta.example.com, add the following to a hosts.py file:

from django_hosts import patterns, host

host_patterns = patterns('path.to',
    host(r'api', 'api.urls', name='api'),
    host(r'beta', 'beta.urls', name='beta'),
)

This causes requests to {api,beta}.example.com to be routed to their corresponding URLconf. You can use your urls.py as a template for these hostconfs.

Patterns are evaluated in order. If no pattern matches, the request is processed in the usual way, ie. using the standard ROOT_URLCONF.

The patterns on the left-hand side are regular expressions. For example, the following ROOT_HOSTCONF setting will route foo.example.com and bar.example.com to the same URLconf.

from django_hosts import patterns, host

host_patterns = patterns('',
    host(r'(foo|bar)', 'path.to.urls', name='foo-or-bar'),
)

Installation

First, install the app with your favorite package manager, e.g.:

pip install django-hosts

Alternatively, use the repository on Github.

You can find the full docs here: django-hosts.rtfd.org

Then configure your Django site to use the app:

  1. Add 'django_hosts' to your INSTALLED_APPS setting.

  2. Add 'django_hosts.middleware.HostsRequestMiddleware' to the beginning of your MIDDLEWARE setting.

  3. Add 'django_hosts.middleware.HostsResponseMiddleware' to the end of your MIDDLEWARE setting.

  4. Create a new module containing your default host patterns, e.g. in the hosts.py file next to your urls.py.

  5. Set the ROOT_HOSTCONF setting to the dotted Python import path of the module containing your host patterns, e.g.:

    ROOT_HOSTCONF = 'mysite.hosts'
  6. Set the DEFAULT_HOST setting to the name of the host pattern you want to refer to as the default pattern. It’ll be used if no other pattern matches or you don’t give a name to the host_url template tag.

Release files for django-hosts 7.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-hosts 7.0.0
File Size Uploaded
django_hosts-7.0.0.tar.gz 36.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-hosts 7.0.0
File Interpreter ABI Platform
django_hosts-7.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 64.7 kB

Release files / django_hosts-7.0.0.tar.gz

Download URL django_hosts-7.0.0.tar.gz
Size 36.9 kB
Tags Source
SHA-256 checksum
How to use checksums
08006bc09f5af69638721ced0657f24044835a22a608732ee1004adc9c95fb7c
BLAKE2b-256 checksum
How to use checksums
eec0cf2b37b6ef4dda89d00f040e69b4f8a3f52fa4f74b33141b68983c0fecce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Release files / django_hosts-7.0.0-py3-none-any.whl

Download URL django_hosts-7.0.0-py3-none-any.whl
Size 27.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93d3c941336463fd0c9377c1773d1c53a49606fa72656ed7ddfa474d2fe1f907
BLAKE2b-256 checksum
How to use checksums
93058056f3e631b0aee8ac99929a558d132634945d3cb16253fb2a2785b62dcf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Release history Release notifications | RSS feed

This release

7.0.0 This release

2 release files

6.0

2 release files

5.2

2 release files

5.1

2 release files

5.0

2 release files

4.0

2 release files

3.0

2 release files

2.0

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

0.6

1 release file

0.5

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4

1 release file

0.3

1 release file

0.2.1

1 release file

0.2

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page