0

I am working on widget for paginator in django framework.

I have the following in the template: When navigating by default the value should be shown as page number, but if a user enters a specific page number it has to navigate to that page number. I am setting value for input box using paginator.number from django Paginator.

If user enters a number, then how can I read that value? Since it was already set value as paginator.number which shows up page number, but how to read a user entered value?

<input type="text" OnChange="location.ref='?page{{ paginator.user_entered }}'" value="{{paginator.number}}">

First << [input-textbof<Page#>] >> Last

More Detailed Code

    <div class="col-md-6 text-left">
        {% if items.has_other_pages %}
                <div class="btn-group" >
                {% if items.has_previous %}
                  <button type="button" class="btn btn-default btn-md page-first" onclick="location.href='?pagenumber=1&limit={{rows}}';">First</button>
                  <button type="button" class="btn btn-default btn-md page-arrows" onclick="location.href='?pagenumber={{ items.previous_page_number }}&limit={{rows}}';">&laquo;</button>
                {% else %}
                  <button type="button" class="btn disabled" >First</button>
                  <button type="button"  class="btn disabled">&laquo;</button>

                {% endif %}
                    </div>
        <span><b>Page </b></span>
                <input type="text" style="width:50px" onchange="location.href='?pagenumber={{items.number}}&limit={{rows}}';" onkeyup="this.onchange()" oninput="this.onchange()" value="{{items.number}}" >
                    <span> of {{items.paginator.num_pages}}</span>


                 <div class="btn-group" >
                {% if items.has_next %}

                  <button type="button"  class="btn btn-default btn-md page-arrows"  onclick="location.href='?pagenumber={{ items.next_page_number }}&limit={{rows}}';">&raquo;</button>
                  <button type="button"  class="btn btn-default btn-md page-last" onclick="location.href='?pagenumber={{ items.paginator.num_pages }}&limit={{rows}}';">Last</button>
                {% else %}

                  <button type="button"  class="btn btn disabled">&laquo;</button>
                    <button type="button" class="btn disabled" >Last</button>

                {% endif %}
                 </div>

        {% endif %}


    </div>
TechJump
  • 79
  • 1
  • 12

1 Answers1

0

In your views, retrieve the page argument like this:

views.py

MAX = 10 # Max items to display
page = request.GET.get("page",1)
if page:
    paginator = Paginator(items,MAX)
    try:
        items = paginator.page(page)
    except PageNotAnInteger:
        items = paginator.page(1)
    except EmptyPage:
        items = paginator.page(paginator.num_pages)

template.html

{% if items.has_other_pages %}
    <div class="pagination">
        {% if items.has_previous %}
                <a href="?page={{items.previous_page_number}}">Previous</a> -
        {% endif %}

        Page {{items.number}} / {{items.paginator.num_pages}}

        {% if items.has_next %}
            - <a href="page={{items.next_page_number}}">Next</a>
        {% endif %}
    </div>
{% endif %}

In case you want to diplay the pagination like a range number: Django paginator page range for not displaying all numbers can help you succeed

Lemayzeur
  • 8,297
  • 3
  • 23
  • 50
  • it is not about retrieving but about sending value for it. – TechJump Apr 24 '18 at 00:26
  • you just want to read the page asked by the user, and then send him the page with a query list. Basically you want to handle the django paginator – Lemayzeur Apr 24 '18 at 01:11
  • I am not sure if I get that , can you please provide some hints This is what exactly I was looking for items.number is the actual page number, it has to be displayed on every page navigation and at the same time, same input box should act as user input to navigate – TechJump Apr 24 '18 at 01:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169634/discussion-between-techjump-and-lemayzeur). – TechJump Apr 24 '18 at 03:20
  • I dont see a way user can enter desired page # here – TechJump Apr 24 '18 at 03:22