Пример #1
0
def reverse(viewname, args=None, kwargs=None, prefix=None, current_app=None,
            host=None, host_args=None, host_kwargs=None,
            scheme=None, port=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.resolvers import reverse
        >>> reverse('about')
        '//www.example.com/about/'
        >>> reverse('about', host='www')
        '//www.example.com/about/'
        >>> reverse('repo', args=('jezdez',), host='www', scheme='git', port=1337)
        'git://jezdez.example.com:1337/repo/'

    You can set the used port and scheme in the host object or override with
    the paramater named accordingly.

    The host name can be left empty to automatically fall back to the default
    hostname as defined in the :attr:`~django.conf.settings.DEFAULT_HOST`
    setting.

    :param viewname: the name of the view
    :param args: the arguments of the view
    :param kwargs: the keyed arguments of the view
    :param prefix: the prefix of the view urlconf
    :param current_app: the current_app argument
    :param scheme: the scheme to use
    :param port: the port to use
    :param host: the name of the host
    :param host_args: the host arguments
    :param host_kwargs: the host keyed arguments
    :raises django.core.urlresolvers.NoReverseMatch: if no host or path matches
    :rtype: the fully qualified URL with path
    """
    host = get_host(host)
    hostname = reverse_host(host,
                            args=host_args,
                            kwargs=host_kwargs)
    path = reverse_path(
        viewname,
        urlconf=host.urlconf,
        args=args or (),
        kwargs=kwargs or {},
        current_app=current_app,
    )
    if scheme is None:
        scheme = host.scheme
    else:
        scheme = normalize_scheme(scheme)

    if port is None:
        port = host.port
    else:
        port = normalize_port(port)

    return iri_to_uri('%s%s%s%s' % (scheme, hostname, port, path))
Пример #2
0
def reverse(viewname, args=None, kwargs=None, prefix=None, current_app=None,
            host=None, host_args=None, host_kwargs=None,
            scheme=None, port=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.resolvers import reverse
        >>> reverse('about')
        '//www.example.com/about/'
        >>> reverse('about', host='www')
        '//www.example.com/about/'
        >>> reverse('repo', args=('jezdez',), host='www', scheme='git', port=1337)
        'git://jezdez.example.com:1337/repo/'

    You can set the used port and scheme in the host object or override with
    the paramater named accordingly.

    The host name can be left empty to automatically fall back to the default
    hostname as defined in the :attr:`~django.conf.settings.DEFAULT_HOST`
    setting.

    :param viewname: the name of the view
    :param args: the arguments of the view
    :param kwargs: the keyed arguments of the view
    :param prefix: the prefix of the view urlconf
    :param current_app: the current_app argument
    :param scheme: the scheme to use
    :param port: the port to use
    :param host: the name of the host
    :param host_args: the host arguments
    :param host_kwargs: the host keyed arguments
    :raises django.core.urlresolvers.NoReverseMatch: if no host or path matches
    :rtype: the fully qualified URL with path
    """
    host = get_host(host)
    hostname = reverse_host(host,
                            args=host_args,
                            kwargs=host_kwargs)
    path = reverse_path(
        viewname,
        urlconf=host.urlconf,
        args=args or (),
        kwargs=kwargs or {},
        current_app=current_app,
    )
    if scheme is None:
        scheme = host.scheme
    else:
        scheme = normalize_scheme(scheme)

    if port is None:
        port = host.port
    else:
        port = normalize_port(port)

    return iri_to_uri('%s%s%s%s' % (scheme, hostname, port, path))