def get_pagination_parameters(request, paginator, paginated):
    """
    Prepare and return the template parameters needed for pagination.

    Thanks to https://gist.github.com/sbaechler/5636351

    Args:
        ``request`` (django.http.HttpRequest): The request object.

        ``paginator`` (django.core.paginator.Paginator): An instance of
        the Paginator with the paginated data.

        ``paginated`` (django.core.paginator.Page): The paginated data.

    Returns:
        ``dict``. A dictionary with all values needed by the template to
        create the pagination.
    """
    LEADING_PAGE_RANGE_DISPLAYED = TRAILING_PAGE_RANGE_DISPLAYED = 10
    LEADING_PAGE_RANGE = TRAILING_PAGE_RANGE = 8
    NUM_PAGES_OUTSIDE_RANGE = 2
    ADJACENT_PAGES = 4

    pages = paginator.num_pages
    page = paginated.number
    in_leading_range = in_trailing_range = False
    pages_outside_leading_range = pages_outside_trailing_range = range(0)
    if pages <= LEADING_PAGE_RANGE_DISPLAYED + NUM_PAGES_OUTSIDE_RANGE + 1:
        in_leading_range = in_trailing_range = True
        page_range = [n for n in range(1, pages + 1)]
    elif page <= LEADING_PAGE_RANGE:
        in_leading_range = True
        page_range = [n for n in range(1, LEADING_PAGE_RANGE_DISPLAYED + 1)]
        pages_outside_leading_range = [
            n + pages for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
    elif page > pages - TRAILING_PAGE_RANGE:
        in_trailing_range = True
        page_range = [n for n in range(
            pages - TRAILING_PAGE_RANGE_DISPLAYED + 1, pages + 1)
            if n > 0 and n <= pages]
        pages_outside_trailing_range = [
            n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]
    else:
        page_range = [n for n in range(
            page - ADJACENT_PAGES, page + ADJACENT_PAGES + 1)
            if n > 0 and n <= pages]
        pages_outside_leading_range = [
            n + pages for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
        pages_outside_trailing_range = [
            n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]

    # Now try to retain GET params, except for 'page'
    params = request.GET.copy()
    if 'page' in params:
        del(params['page'])
    get_params = params.urlencode()
    prev = paginated.previous_page_number() if paginated.has_previous() else ""

    return {
        'pages': pages,
        'page': page,
        'previous': prev,
        'next': paginated.next_page_number() if paginated.has_next() else "",
        'has_previous': paginated.has_previous(),
        'has_next': paginated.has_next(),
        'page_range': page_range,
        'in_leading_range': in_leading_range,
        'in_trailing_range': in_trailing_range,
        'pages_outside_leading_range': pages_outside_leading_range,
        'pages_outside_trailing_range': pages_outside_trailing_range,
        'get_params': get_params,
        'count': paginator.count,
    }