skip to navigation
skip to content

django-hosts 1.2

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

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

For example, if you own but want to serve specific content at and, add the following to a file:

from django_hosts import patterns, host

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

This causes requests to {api,beta} to be routed to their corresponding URLconf. You can use your 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 and to the same URLconf.

from django_hosts import patterns, host

host_patterns = patterns('',
    host(r'(foo|bar)', '', name='foo-or-bar'),


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:

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 file next to your

  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.

File Type Py Version Uploaded on Size
django-hosts-1.2.tar.gz (md5, pgp) Source 2015-05-06 30KB
django_hosts-1.2-py2.py3-none-any.whl (md5, pgp) Python Wheel py2.py3 2015-05-06 27KB
  • Downloads (All Versions):
  • 127 downloads in the last day
  • 988 downloads in the last week
  • 4267 downloads in the last month