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/travis/jezdez/django-hosts.svg https://img.shields.io/coveralls/jezdez/django-hosts.svg https://readthedocs.org/projects/django-hosts/badge/?version=latest&style=plastic

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_CLASSES setting.

  3. Add 'django_hosts.middleware.HostsResponseMiddleware' to the end of your MIDDLEWARE_CLASSES 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-1.2.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

django_hosts-1.2-py2.py3-none-any.whl (28.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django-hosts-1.2.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-hosts-1.2.tar.gz
Algorithm Hash digest
SHA256 dc07a126f135640cb01281788658abedab570e2a7b0620a0ed33f2cdf8a88dad
MD5 fe386a7e2855114a59d56f88bb38704a
BLAKE2b-256 10cc2b97a4850f7a15e0bb18e8672c4651062710710fea0e87bc7e958906979e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for django_hosts-1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 45f1fcb1b07a93a30cbe211c81e79d25138c13e6dd829d97aec9d9a2eb8b93b8
MD5 50170cc20c6b5b2f655ddddb69bf5417
BLAKE2b-256 adcf8cac23f582d02404fe59ecf5d63d1d7c5b8f71312f5dda607432fce0f78b

See more details on using hashes here.

Provenance

Supported by

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