Z
Z
zelsky2015-04-29 15:13:48
JavaScript
zelsky, 2015-04-29 15:13:48

Loading Django content?

How can I implement automatic content loading like in twitter?
A simple model for a blog. Just to test.
Title of the article, Text of the article.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zigen, 2015-04-29
@zelsky

I used Django-endless-pagination in the project.
It is easily configured and integrated into the view.
Loads on scroll - like on Twitter. There is a twitter style for this
https://django-endless-pagination.readthedocs.org/...

K
kazmiruk, 2015-04-29
@kazmiruk

Junga has nothing to do with it. You make an api (django-rest-framework can be used), and then track the scroll on the page with javascript. As soon as the scroll has reached the lower border, you call the api with a request for the next piece of data. After receiving the data, draw it.

A
Alexey Sergeev, 2015-04-29
@SergeevAI

I would do the following
1) 1st option
in views

from django.core import serializers
from .models import MyModel

def my_view(request):
    if request.is_ajax():
        x = request.GET.get('value', 5)
        data = serializers.serialize('json', MyModel.objects.all()[:x] )   #тут по вкусу, можно все объекты, можно частями.
        return HttpResponse(data, content_type='application/json')
    else:
        return render(request, 'template.html')

2) 2nd option is to use Django-Rest-Framework
And in templates already jQuery-Ajax, Angular etc.
But I myself am still quite new to this business, so I can be wrong :)
I found this option:
$(window).scroll(function() {   
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       $.ajax({
            type: 'GET',
            url: '{% url 'app:json_view' %}', //Ссылка на вьюху
            dataType: "json",
            data: {'value': 10},  //Здесь можно передать данные в GET запросе, например сколько значений получить 
            success: function(data) {
                // Ответ приходит в переменную data. Её и рендерим на страницу
            }
        }); 
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question