On Monday, 23 January 2012 04:40:31 UTC, Swaroop Shankar wrote: > > Hi, > Am trying to create a custom pagination for my project, but am seriously > struck with the django implementation of Limit and offset. I went through > the documents, instead of making me understand how it works, i ended up > more confused. So suppose I have a page limit of 3 records per page and if > i want to load the second set of records how should the following statement > be? > > Data = NewsFeedStory.objects.order_by('-updated_on')[?:?] > > and similarly if i want to load the 3rd set with same page limit, how > should the same statement be? I played with the django shell to figure out > how this works but am still confused. Please help. > > Thanks and Regards, > > Swaroop Shankar V > > I'm not sure why this should confuse you. You seem to have correctly understood the syntax.
This is just normal Python slicing, which Django transforms into LIMIT and OFFSET. So, remembering that Python slicing is 0-based and the lower bound is inclusive but the upper is exclusive, the first page is [:3], the second page is [3:6], the third is [6:9], etc. Note, however, that if you're doing pagination, you should really be using the built-in Paginator class [1], which calculates all this for you given a page number, rather than doing it yourself. [1]: https://docs.djangoproject.com/en/1.3/topics/pagination/ -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/NDDsTmr8iDoJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.