0

I am using Pagination in Django but using AJAX so I have to send all variables values from view to AJAX call. But For Current page there is no builtin variable available ?. As I saw official documentation. So how to Send this data already calculated in view.py ?

 <span class="current">
        Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
    </span>

Link referenceFollowing this example

Tousif
  • 50
  • 1
  • 12

3 Answers3

0

The only sensible way to do pagination/ajax/django template tags, would be to generate the entire table + current page data + table navigation etc. in the django view. I.e. move all the variables for the table from your containing page to the view for the table.

Probably a better solution is to find yourself a javascript table component and have django serve it data...

thebjorn
  • 26,297
  • 11
  • 96
  • 138
0

Instead of creating two different views, you can deliver the paginated content from the same view by adding a GET parameter to the url, to check for the page number and sending the ajax request to the same view. This way, it'll be easier to manage one view instead of two for the same content. And if your view does much more than generating the particular content, as you are using ajax, you can easily split the view such that one view delivers only the related content.

For example, if the url of your view is \url-to-somepage you can send the ajax request to \url-to-somepage?page=2

Then in your template file, say template.html, include another template, say __sub_template.html for the content which will be paginated. Like,

<div>
    <!--
        rest of the page
    -->
    {% include 'templates\__sub_template.html' %}
</div>

Then in your view,

.def your_view(request):
    """
    Your code here
    """
    paginator = Paginator(contacts, number)
    page = request.GET.get('page')
    try:
        result_list = paginator.page(page)
    except PageNotAnInteger:
        result_list = paginator.page(1)
    except EmptyPage:
        result_list = []
    if page:
        return render(request, '__sub_template.html', {'contacts': result_list})
    else:
        return render(request, 'template.html', {'contacts': result_list})
aRtST
  • 46
  • 1
  • 4
0

Use Django endless pagination http://django-endless-pagination.readthedocs.io/en/latest/twitter_pagination.html#pagination-on-scroll

Sandeep Balagopal
  • 1,943
  • 18
  • 28
  • Is there any working Example of it ? I am getting confuse as is I didnt know how it will work in my case. – Tousif Apr 18 '17 at 05:10
  • The example inside documentation works. Basically you will need two templates. and you have to include template, use the scripts etc. everything is there. – Sandeep Balagopal Apr 18 '17 at 05:17