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 host URL conf file. Host URL conf file should define all routes which going to be hendled on a certain subdomain and has a same format like your plain urls.py (you can use it as a tamplate for your host URL confs). For example, requests to api.example.com will be mapped to views acording to /path/to/api/urls.py file
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'),
)
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:
Add
'django_hosts'to yourINSTALLED_APPSsetting.Add
'django_hosts.middleware.HostsRequestMiddleware'to the beginning of yourMIDDLEWAREsetting.Add
'django_hosts.middleware.HostsResponseMiddleware'to the end of yourMIDDLEWAREsetting.Create a new module containing your default host patterns, e.g. in the
hosts.pyfile next to yoururls.py.Set the
ROOT_HOSTCONFsetting to the dotted Python import path of the module containing your host patterns, e.g.:ROOT_HOSTCONF = 'mysite.hosts'
Set the
DEFAULT_HOSTsetting 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 thehost_urltemplate tag.