Reference

hosts and patterns

django_hosts.defaults.patterns(prefix, *args)

The function to define the list of hosts (aka host confs), e.g.:

from django_hosts import patterns

host_patterns = patterns('path.to',
    (r'www', 'urls.default', 'default'),
    (r'api', 'urls.api', 'api'),
)
Parameters:
  • prefix (str) – the URLconf prefix to pass to the host object
  • *args – a list of hosts instances or an iterable thereof
class django_hosts.defaults.host(regex, urlconf, name, callback=None, prefix='')

The host object used in host conf together with the django_hosts.defaults.patterns() function, e.g.:

from django_hosts import patterns, host

host_patterns = patterns('path.to',
    host(r'www', 'urls.default', name='default'),
    host(r'api', 'urls.api', name='api'),
)
Parameters:
  • regex (str) – a regular expression to be used to match the request’s host.
  • urlconf (str) – the dotted path of a URLconf module of the host
  • callback (callable or str) – a callable or the dotted path of a callable to be used when matching has happened
  • prefix (str) – the prefix to apply to the urlconf parameter
add_prefix(prefix='')

Adds the prefix string to a string-based urlconf.

Reversal with reverse_host and reverse_full

django_hosts.reverse.reverse_full(host, view, host_args=None, host_kwargs=None, view_args=None, view_kwargs=None)

Given the host and view name and the appropriate parameters, reverses the fully qualified URL, e.g.:

>>> from django.conf import settings
>>> settings.ROOT_HOSTCONF = 'mysite.hosts'
>>> settings.PARENT_HOST = 'example.com'
>>> from django_hosts.reverse import reverse_full
>>> reverse_full('www', 'about')
'//www.example.com/about/'
Parameters:
  • host – the name of the host
  • view – the name of the view
Host_args:

the host arguments

Host_kwargs:

the host keyed arguments

View_args:

the arguments of the view

View_kwargs:

the keyed arguments of the view

Return type:

fully qualified URL with path

django_hosts.reverse.reverse_host(host, args=None, kwargs=None)

Given the host name and the appropriate parameters, reverses the host, e.g.:

>>> from django.conf import settings
>>> settings.ROOT_HOSTCONF = 'mysite.hosts'
>>> settings.PARENT_HOST = 'example.com'
>>> from django_hosts.reverse import reverse_host
>>> reverse_host('with_username', 'jezdez')
'jezdez.example.com'
Parameters:name – the name of the host as specified in the hostconf
Args:the host arguments to use to find a matching entry in the hostconf
Kwargs:similar to args but key value arguments
Raises django.core.urlresolvers.NoReverseMatch:
 if no host matches
Return type:reversed hostname

HostSiteManager model manager

class django_hosts.managers.HostSiteManager(field_name=None, select_related=True)

A model manager to limit objects to those associated with a site.

Parameters:
  • field_name – the name of the related field pointing at the Site model, or a series of relations using the field1__field2__field3 notation. Falls back to looking for ‘site’ and ‘sites’ fields.
  • select_related – a boolean specifying whether to use select_related() when querying the database

Define a manager instance in your model class with one of the following notations:

on_site = HostSiteManager()  # automatically looks for site and sites
on_site = HostSiteManager("author__site")
on_site = HostSiteManager("author__blog__site")
on_site = HostSiteManager("author__blog__site",
                          select_related=False)

Then query against it with one of the manager methods:

def home_page(request):
    posts = BlogPost.on_site.by_request(request).all()
    return render(request, 'home_page.html', {'posts': posts})
by_id(site_id=None)

Returns a queryset matching the given site id. If not given this falls back to the SITE_ID setting.

Parameters:site_id – the ID of the site
Return type:QuerySet
by_request(request)

Returns a queryset matching the given request’s site attribute.

Parameters:request (HttpRequest) – the current request
Return type:QuerySet
by_site(site)

Returns a queryset matching the given site.

Parameters:site (Site) – a site instance
Return type:QuerySet