For example i will use news app.
To install Lazy Load script first of all need to import some django modules. In views.py add 2 rows on the top:
from django.http import JsonResponse
from django.template.loader import render_to_string
2.In page function, before return, we need to make check if request ask for lazy load or not.
if request.method == "GET" and "lazy_load" in request.GET:
lazy_html = render_to_string('news/parts/_list.html', context, request)
return JsonResponse({'html':lazy_html})
else:
return render(request, 'news/index.html', context)
This page must have pagination!!!
Prepare html template. List of news must be in another file without conection of header and footer. In this example i made file ‘_list.html’:
{{ post.post_date | date:"d M Y" }}
/
{{ post.author }}
/
{% for cat in post.categories.all %}{% if forloop.counter0 > 0 %},{% endif %}
{{cat}}
{% endfor %}
/
{{ post.comments_count }} Comments
/
{{ post.preview_text }}
{% elif post.post_type == 'vd' %}
{% endif %}
{{ post.post_date | date:"d M Y" }}
/
{{ post.author }}
/
{% for cat in post.categories.all %}{% if forloop.counter0 > 0 %},{% endif %}
{{cat}}
{% endfor %}
/
{{ post.comments_count }} Comments
/
{{ post.preview_text }}
{% elif post.post_type == 'gl' %}
{% endif %}
{% if post.gallery_photo_2.url %}
{% endif %}
{% if post.gallery_photo_3.url %}
{% endif %}
{% if post.gallery_photo_4.url %}
{% endif %}
{% else %}
{% endif %}
{% if post.gallery_photo_2.url %}
{% endif %}
{% if post.gallery_photo_3.url %}
{% endif %}
{% if post.gallery_photo_4.url %}
{% endif %}
{% endif %}
{{ post.title }}
{{ post.post_date | date:"d M Y" }}
/
{{ post.author }}
/
{% for cat in post.categories.all %}{% if forloop.counter0 > 0 %},{% endif %}
{{cat}}
{% endfor %}
/
{{ post.comments_count }} Comments
/
{{ post.preview_text }}
{% elif post.post_type == 'qt' %}
{{ post.preview_text }}
{{ post.post_date | date:"d M Y" }}
/
{{ post.author }}
/
{% for cat in post.categories.all %}{% if forloop.counter0 > 0 %},{% endif %}
{{cat}}
{% endfor %}
/
{{ post.comments_count }} Comments
/
{% else %}
ERROR
{% endif %}
{% endfor %}
{% else %}
No news
{% endif %}
Important to remember - container of news list must be not in this file but “Show More“ link must be in.
In this way we will not recieve container with same id we have and we can update “Show More“ link after each request.
index.html of news app will be like this
....
4.Show More“ link can be any element. Important only to set onclick function (or other like - onsubmit, onhover, etc.) with 3 parametrs - lazyLoadFunc(url, containerId, linkContainerId):
1 - URL of next page.
2 - Id of news list container
3 - Id of 'show more' link container
JS file.
//Lazy Load function
//Options: 1 - url where need to send request like '?page=2'
// 2 - Id of container where data must be placed
// 3 - Id of contaniner where 'show more' link is.
function lazyLoadFunc(url, containerId, linkContainerId){
var r = new XMLHttpRequest();
r.addEventListener('readystatechange', function(event){
if (this.readyState === 4){
if (this.status === 200){
//get answer and parse it
var data = parseJson(r.responseText);
//if not empty answer
if(!!data){
//remove old link
var linkContainer = document.getElementById(linkContainerId);
linkContainer.remove();
//insert new data to container
var container = document.getElementById(containerId);
container.insertAdjacentHTML('beforeend', data.html);
}
}
}
})
r.open('get', url+'&lazy_load=True' , true);
r.send();
}
Do not forget to include it in base.html file.