2

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 (e.g. django-pagination) mainly deal with pagination resulting from a database query.

Community
  • 1
  • 1
super9
  • 29,181
  • 39
  • 119
  • 172

2 Answers2

3

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.

Matt Howell
  • 15,750
  • 7
  • 49
  • 56
  • Sorry Im still not too sure how to do it. The thing is, for my to read the objects into the list, I need to pass it the the page number. How do I auto-generate this page number in the first place? – super9 Mar 16 '11 at 09:36
  • @Nai: Read **all** of the objects. Then let Django's paginator do the rest of the work for you. Please read the code sample. It works for **any** list. So get **all** items from your other service. The Django page number information is described completely in the associated link. – S.Lott Mar 16 '11 at 10:08
  • @S.Lott So assuming there are 1000 images, I have to download all 1000 of them first to get the final count so I can calculate the number of pages that result from it? Is this the best way of doing it? Can I not make the request on the fly? – super9 Mar 16 '11 at 10:19
  • @Nai: "I have to download all 1000 of them first to get the final count so I can calculate the number of pages that result from it?". No. You need a list of some kind. Downloading all of them is one simple way to get a list. Another is to write a generator function. Start with downloading all so that you can get *something* to work. Later, you can modify it. – S.Lott Mar 16 '11 at 10:26
0

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.

tiagoboldt
  • 2,426
  • 1
  • 24
  • 30
  • What kind of code snippets would be helpful in this situation? I dont't know providing my views.py or my template file would be useful? – super9 Mar 16 '11 at 09:37