0

I am trying to paginate my products catalog using paginate() function that comes with Flask .

Inside catalog i am using a search filter to sort products depends on the filter itself, in the url i have more than 5 to 10 arguments, these arguments are always changing and i want the paginate path to contain them .

Inside template i am doing this:

{% if pagination.has_next or pagination.has_prev %}
{{ 
    macros.pagination_widget(
        pagination,
        request.endpoint,
        args=request.view_args
    )
}}
{% endif %}

The issue is, request.view_args always returns an empty dictionary , in fact the arguments are in url .

If i tried to hardcode all the arguments by using just some logic like {% if %} and {% else %} which i don't want to, the pagination works just fine .

swordfish
  • 959
  • 16
  • 39
  • Does your view define those arguments? – Mekicha Jun 12 '18 at 08:50
  • @Mekicha no it doesn't , do i have ? i mean i can get arguments from `request.args` for example without defining them inside my view ! – swordfish Jun 12 '18 at 09:00
  • `request.args ` accesses the parsed URL parameters (the part in the URL after the question mark). `view_args `is A dict of view arguments that matched the request, for instance if you have a view function that takes a user_id variable like this: `def get_user(user_id)`. – Mekicha Jun 12 '18 at 09:19
  • Oh i see, so `request.view_args` working just if you are passing some arguments to your view . – swordfish Jun 12 '18 at 09:21

1 Answers1

0

I solved the problem, by using request.query_string and creating a macro helper everything now is working just fine.

Here is the solution:

_macros_pagination.html

{% macro pagination_widget(pagination, endpoint, extra='') %}
    <ul class="uk-pagination uk-flex-center" uk-margin="">
        <li{% if not pagination.has_prev %} class="disabled"{% endif %}>
            <a href="{% if pagination.has_prev %}{{ url_for(endpoint, page=pagination.prev_num, **kwargs) }}&{{extra}}{% else %}#{% endif %}">
                <span uk-pagination-previous=""></span>
            </a>
        </li>
        {% for p in pagination.iter_pages() %}
            {% if p %}
                {% if p == pagination.page %}
                <li class="uk-active">
                    <a href="{{ url_for(endpoint, page = p, **kwargs) }}&{{extra}}">{{ p }}</a>
                </li>
                {% else %}
                <li>
                    <a href="{{ url_for(endpoint, page = p, **kwargs) }}&{{extra}}">{{ p }}</a>
                </li>
                {% endif %}
            {% else %}
            <li class="uk-disabled"><span>...</span></li>
            {% endif %}
        {% endfor %}
        <li{% if not pagination.has_next %} class="disabled"{% endif %}>
            <a href="{% if pagination.has_next %}{{ url_for(endpoint, page=pagination.next_num, **kwargs) }}&{{extra}}{% else %}#{% endif %}">
                <span uk-pagination-next=""></span>
            </a>
        </li>
    </ul>
{% endmacro %}

template.html

{% import "_macros_pagination.html" as macros %}

{% if pagination.has_next or pagination.has_prev %}
    {{ 
        macros.pagination_widget(
            pagination,
            request.endpoint,
            extra=request.query_string
        )
    }}
{% endif %}
swordfish
  • 959
  • 16
  • 39