S
S
Sergey08082017-10-16 11:40:12
Django
Sergey0808, 2017-10-16 11:40:12

Python Django, how to break all news into parts?

My view lists the top 12 news. On the news list page, I implemented the ability to load the rest of the news ajax'om. But the problem is that all the rest are displayed.
Here is an example of a view that loads news:

def lazy_ajax(request):
    if request.is_ajax():
        news_list = News.objects.all()[12:]
        posts_html = render_to_string(template_name='news/list_ajax.html', context={'news_list': news_list})
        output_data = {'posts_html': posts_html}
        return JsonResponse(output_data, content_type='application/json', safe=False)

How to display when loading the page, that is, when accessing this view, display not the entire list, but 12 news?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Sovetnikov, 2017-10-16
@Sovetnikov

Instead of "[12:]" you need "[:12]", or rather, even "[12:12]" if you need a second page, but where is the page number parameter?

A
AlexandrBirukov, 2017-10-16
@AlexandrBirukov

As an option, I will suggest not writing your bike - django-el-pagination

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question