Skip to main content

Dynamic and static host resolving for Django. Maps hostnames to URLconfs.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-hosts-5.0.tar.gz (35.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_hosts-5.0-py2.py3-none-any.whl (27.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-hosts-5.0.tar.gz.

File metadata

  • Download URL: django-hosts-5.0.tar.gz
  • Upload date:
  • Size: 35.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for django-hosts-5.0.tar.gz
Algorithm Hash digest
SHA256 e917f8f55a144cee305cbf3a04656f94174cdcf0dddbc2cd5a110a3e842746dc
MD5 4fdf18ff690b05b0731e58ff4ee4df2c
BLAKE2b-256 485622dcca25c3fb7dc0dbe7b3a2bae5dee85d79484dae8f66cee50b00a1f1a8

See more details on using hashes here.

File details

Details for the file django_hosts-5.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_hosts-5.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for django_hosts-5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a45492c50b0a87f583074360f87d2fff97a954188a12cc942cf87e3e241dc7aa
MD5 968bf459e87d8384129324b8275cba3c
BLAKE2b-256 a60b05f0452881cd95076f075e68c69cb044da2b11612b80ae4b10d3f9cf45ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page