I've turned to the experts at stackoverflow in hopes I could figure this out.
Let's look at a popular application like Instagram for example. Let's say you decide to look for posts with the hashtag #love (about 551,677,074 posts and growing every second!)
Each page contains 15 results in descending order of post time. As you scroll down and load more, it returns the next set of 15 results and so on. One might ask, well, if the queryset is growing every second, by the time I'm ready to view page 2, how can I be sure that page 2 will contain the next set of results in order relative to the results I just got in page 1? If the queryset is growing, isn't there a chance that I might see some or all of the results I previously got from page 1? In the eyes of the user, its as if the queryset is constantly being pushed forward relative to where they we're last time.
Well that right there is my dilemma folks. If the queryset is growing, how can assure that the next page I request will start off from where I left off on page 1?
I've created the following model below:
class Blog(models.Model):
author = models.ForeignKey(User)
published_time = models.DateTimeField(auto_now_add=True)
text = models.CharField(max_length=1000, blank=True)
I would like to create a view that returns 10 blogs objects at a time ordered by published_time.
from django.core.paginator import Paginator
def BlogView(request):
if request.META.has_key('HTTP_AUTHORIZATION') and request.method == 'GET':
authmeth, auth = request.META['HTTP_AUTHORIZATION'].split(' ', 1)
if authmeth.lower() == 'basic':
auth = auth.strip().decode('base64')
username, password = auth.split(':', 1)
authuser = authenticate(username=username, password=password)
if authuser:
if authuser.is_active:
queryset = Blog.objects.all().order_by('-published_time') #descending order
paginator = Paginator(queryset, 10) # 10 objects per page
blogs = paginator.page(page_number)
data = []
for blog in blogs:
data.append(
{'id': blog.pk,
'author_id': blog.author_id,
'text': blog.text
'published_time': blog.published_time})
return HttpResponse(json.dumps({'results':data}), content_type="application/json")
Very simple setup, yet, I can't figure out on how to do this, any suggestions and examples would extremely be appreciated!