0

I'm attempting to fix some pagination issues in a Django app. Using the Previous and Next buttons work fine, since I'm using existing variables like previous_page_number, but I'm unsure how to get the Page 1, Page 2, Page N buttons to work. Here's the Previous Page code, which works just fine:

{% if table.page and table.paginator.num_pages > 1 %}
    {% block pagination %}
    <nav class="pagination is-centered" role="navigation" aria-label="pagination">
        {% block pagination.previous %}
        {% if table.page.has_previous %}
            <a href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}" class="pagination-previous">Previous</a>
        {% else %}
            <a href="#" class="pagination-previous" disabled>Previous</a>
        {% endif %}
        {% endblock pagination.previous %}

And here's my current Pagination Range code, which doesn't. Specifically, the else segment is what I'm unable to get working.

{% block pagination.range %}
{% if table.page.has_previous or table.page.has_next %}
    <ul class="pagination-list">
    {% for p in table.page|table_page_range:table.paginator %}
        {% if table.page.number == p %}
        <li>
            <a class="pagination-link is-current is-dark" aria-label="Page {{ table.page.number }}" aria-current="page">{{ table.page.number }}</a>
        </li>
        {% else %}
        <li>
            <a href="{% querystring table.prefixed_page_field=table.page.p %}" class="pagination-link" aria-label="Page {{ p }}">{{ p }}</a>
       </li>
       {% endif %}
   {% endfor %}
   </ul>
{% endif %}
{% endblock pagination.range %}

Currently, the pagination doesn't work, and the URL that gets passed ends at page= instead of page=p, whatever p is at the time. I'm uncertain how to pass p in as a readable variable, to cause it to appear in the URL. I've tried using the same format as other parts of the code not in the querystring.

{% else %}
<li>
    <a href="{% querystring table.prefixed_page_field=table.page.{{ p }} %} class="pagination-link" aria-label="Page {{ p }}">{{ p }}</a>
   </li>
{% endif %}

This causes an error that prevents the page loading at all, as Django can't reconcile the {{ part.

  • 1
    Hi Jay, this is a possible duplicate of https://stackoverflow.com/questions/13387885/dynamic-name-variable-on-django-template?lq=1 – nicowernli Sep 11 '18 at 13:17
  • You're right. Can I verify a comment as the best answer, and if so, how should I do that? – Jay Bailey Sep 11 '18 at 13:52
  • I don't think thats possible, you just can mark a comment as usefull click in the up-arrow next to the comment. Regards! – nicowernli Sep 11 '18 at 13:59

1 Answers1

0
<a href="yourlink.domain/?page=1">Display</a> 

and you can get from the views.py like that;

page = request.GET.get("page", ""), 

if it is page is none you can give them default value like this

page = request.GET.get("page", "1")
jackquin
  • 534
  • 1
  • 7
  • 19