- views.py
posts = Post.objects.all()
page = request.GET.get('page')
paginator = Paginator(posts, 1)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = []
if request.headers.get("x-request-with") == "XMLHttRequest":
return render(request, "social/posts-fragment.html", {"posts": posts})
context = {'posts': posts}
return render(request, "social/posts-list.html", context)
- список сообщений (текущая страница)
{% block title%} posts list {%endblock%}
{% block content %}
{% for post in posts %}
{{post.description}}
Published at {{post.created}} by {{post.author}}
{% endfor %}
load more posts
var page = 2;
function loadMore(){
var url = '{% url "social:post_list" %}' + '?page=' + page;
fetch(url, {
method: 'GET',
headers: {
'content-type': 'text/html', 'x-requested-with': 'XMLHttpRequest'
},
}).then(function(response){
return response.text();
}).then(function(html){
document.querySelector(".posts").insertAdjacentHTML("beforeend", html);
page++;
})
}
var btnLoadMore = document.querySelector(".load-more");
btnLoadMore.addEventListener("click", loadMore);
{% endblock %}
Подробнее здесь: https://stackoverflow.com/questions/798 ... -fetch-api
Мобильная версия