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