doniyor
doniyor

Reputation: 37896

django - load more on scroll - i have pagination, how do i load more?

i want to write a code which fetches more data from db when i come to bottom of page.

i have this views.py:

locations = Location.objects.filter(**s_kwargs).distinct('id')
paginator = Paginator(locations, 10)
if  request.GET.get('page'):
   locs = paginator.page(request.GET.get('page'))
else:
   locs = paginator.page(1)
data['locs'] = locs
return render_to_response("result.html",data,context_instance=RequestContext(request)) 

this is my js for detecting the bottom of my page:

 $(window).scroll(function () {
  if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
    alert("start loading more data");//<--- load more data from db. 
  }
 });

I am stuck in my thoughts not knowing how to do this, paginator is confusing me. how do i fetch the next data from db? how do i attach the new coming data to existing data in html? i need your guidance please

Upvotes: 3

Views: 1576

Answers (1)

arghbleargh
arghbleargh

Reputation: 3170

It looks like you want to make an AJAX request in the javascript for the next page. In your views.py, you would want to do something like

return HttpResponse(json.dumps(data), mimetype="application/json")

instead of returning an HTML page as you do now. (If you also need that controller to serve the original page, maybe you can have an optional query parameter that tells the server to give a JSON response instead of the usual HTML.)

Upvotes: 5

Related Questions