简体   繁体   中英

How to do paging/pagination in Django for 3rd party REST services

To use flickr as an example, a request URL looks something like this:

'http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=' + settings.FLICKR_API_KEY + '&user_id=' + userid + '&format=json&per_page' + per_page + '&page=' + page + '&nojsoncallback=1'

where page controls which page to display and per_page controls the number of photos to return

To simplify matters, let's make per_page fixed. So my question is, how can I implement a paging system that allows a user to go one page forwards or back at anytime on the webpage?

I imagine I would need to pass the page number to iterate through the request URL such that the right data is displayed. SO I guess I'm not sure how to tie the template to the views.py. Essentially, I'm looking for the Django version of this Rails question .

The examples and plugins I have come across so far (eg django-pagination) mainly deal with pagination resulting from a database query.

Django-pagination will work with any list of objects -- not just calls from the database. The example here actually starts off with an example that has nothing to do with local models or databases.

For an API-type call, you'd just need to read your objects into a list, and then create a new Paginator objects based off of that list. All you have to do is give it the number of objects you want per page. It's really very simple.

With your description, I would say start with page = 1. If page = 1, have one link in your page to go forward, that will load page = 2. If you're in any other page other than one generate two links, one for the previous and other for the next page, relative to the current one.

If you want better help you really have to show us some code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM