简体   繁体   English

如何在Django中为第三方REST服务进行分页/分页

[英]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: 以flickr为例,请求URL如下所示:

'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 其中page控制要显示的页面,而per_page控制要返回的照片数

To simplify matters, let's make per_page fixed. 为简化起见,让我们固定per_page 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. 我想我需要传递页码来遍历请求URL,以便显示正确的数据。 SO I guess I'm not sure how to tie the template to the views.py. 所以我想我不确定如何将模板绑定到views.py。 Essentially, I'm looking for the Django version of this Rails question . 本质上,我正在寻找该Rails问题的Django版本。

The examples and plugins I have come across so far (eg django-pagination) mainly deal with pagination resulting from a database query. 到目前为止,我遇到的示例和插件(例如django-pagination)主要处理数据库查询产生的分页。

Django-pagination will work with any list of objects -- not just calls from the database. Django-pagination将与任何对象列表一起使用-不仅仅是从数据库中调用。 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. 对于API类型的调用,您只需要将对象读入列表,然后根据该列表创建一个新的Paginator对象。 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. 就您的描述而言,我想说的是从page = 1开始。如果page = 1,则页面中有一个链接要前进,这将加载page =2。如果您在除1之外的任何其他页面中,则生成两个链接,相对于当前页面,一个用于上一页,另一个用于下一页。

If you want better help you really have to show us some code. 如果您想获得更好的帮助,则必须向我们展示一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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