<script type="text/javascript">
if (history.pushState) {
var baseUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
var newUrl = baseUrl + `?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page }}&size={{ size }}&order={{ order }}&direction={{ direction }}`;
history.replaceState(null, null, newUrl);
}
</script>
<script type="text/javascript">
function catSelect(event) {
var cat_id = $(event.currentTarget).attr('cat_id');
if (event.ctrlKey) {
window.location.href = "/publication/?cat={{ cat_id_join }}," + cat_id + "{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page=1&size={{ size }}&order={{ order }}&direction={{ direction }}"
return;
}
if (event.altKey) {
window.location.href = "/publication/?cat={{ cat_id_join }}," + cat_id + "(del){% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page=1&size={{ size }}&order={{ order }}&direction={{ direction }}"
return;
}
window.location.href = "/publication/?cat=" + cat_id + "&page=1&size={{ size }}&order={{ order }}&direction={{ direction }}"
}
</script>
<script type="text/javascript">
window.need_page={{ page|add:"1" }};
window.addEventListener('scroll', function() {
let windowRelativeBottom = document.documentElement.getBoundingClientRect().bottom;
if (windowRelativeBottom < document.documentElement.clientHeight + 100) {
if (window.need_load_more == 1) {
window.need_load_more = 0;
var myDivPaginator = document.getElementById("paginatorTop");
var myDivContent = document.getElementById("publication_content_block");
$.ajax({
url: "/ajaxMorePub/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page=" + window.need_page + "&size={{ size }}&order={{ order }}&direction={{ direction }}",
success: function(response){
var parser = new DOMParser();
var ajax_html = parser.parseFromString(response, "text/html");
var container = ajax_html.getElementsByClassName("container")[0];
if (container.id != "empty") {
myDivContent.insertAdjacentHTML('beforeEnd', ajax_html.getElementById("pubcontent").outerHTML);
myDivPaginator.innerHTML = ajax_html.getElementById("paginatorTop").outerHTML;
if (history.pushState) {
var baseUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
var newUrl = baseUrl + `?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page=` + window.need_page + `&size={{ size }}&order={{ order }}&direction={{ direction }}`;
history.pushState(null, null, newUrl);
}
window.need_page = window.need_page + 1;
window.need_load_more = 1;
}
else { var text_on_spinner = document.getElementById("text_on_spinner");
text_on_spinner.innerHTML = "Это все публикации, загляните позже или измените фильтр!!!";
var spinner = document.getElementById("spinner_class");
spinner.className="alert alert-danger";
return;
}
}
});
};
};
}); </script>
from django.core.paginator import Paginator
====================================
# получаем список pub_id по параметрам
pubidlist=get_pub_id_by_params(cat_id=set(cat_id), tag=set(tag),
order=order, direction=direction)
# осуществляем пагинацию
paginator = Paginator(pubidlist, size)
if page>paginator.num_pages:
if template=="ajaxMorePub.html":
return render(request, "emptypublist.html")
page=paginator.num_pages
pubidlist = paginator.get_page(page)
# запрашиваем модель данных
pubmodel=get_pub_model(pubidlist,
order=order, direction=direction)
====================================
response = render(request, template, {
'pub_sorted': pubmodel.items(),
'order': order, 'size': size, 'direction': direction,
'page_range': paginator.page_range,
'num_pages': paginator.num_pages,
'paginator': paginator.page(page),
'page': page
})
{% if pub_sorted|length != 0 %}
<nav aria-label="pagination">
<ul class="pagination justify-content-center">
<!-- Первая страница -->
<li class="page-item {% if page == 1 %}disabled{% endif %}"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page=1&size={{ size }}&order={{ order }}&direction={{ direction }}">Первая</a> </li>
<!-- Если существует предыдущая страница --> {% if paginator.has_previous %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ paginator.previous_page_number }}&size={{ size }}&order={{ order }}&direction={{ direction }}">«</a> </li>
{% else %} <li class="page-item disabled"> <a class="page-link" href="#">«</a> </li>
{% endif %} <!-- Если нет страницы +1, но есть страница -6 --> {% if page|add:"1" not in page_range and page|add:"-6" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"-6" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"-6" }}</a> </li>
{% endif %} <!-- Если нет страницы +2, но есть страница -5 --> {% if page|add:"2" not in page_range and page|add:"-5" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"-5" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"-5" }}</a> </li>
{% endif %} <!-- Если нет страницы +3, но есть страница -4 --> {% if page|add:"3" not in page_range and page|add:"-4" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"-4" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"-4" }}</a> </li>
{% endif %} <!-- Если существует страница текущая - 3 --> {% if page|add:"-3" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"-3" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"-3" }}</a> </li>
{% endif %} <!-- Если существует страница текущая - 2 --> {% if page|add:"-2" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"-2" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"-2" }}</a> </li>
{% endif %} <!-- Если существует страница текущая - 1 --> {% if page|add:"-1" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"-1" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"-1" }}</a> </li>
{% endif %} <!-- Текущая активная страница -->
<li class="page-item active"> <a class="page-link" href="#">{{ page }}</a> </li>
<!-- Если существует страница текущая + 1 --> {% if page|add:"1" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"1" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"1" }}</a> </li>
{% endif %} <!-- Если существует страница текущая + 2 --> {% if page|add:"2" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"2" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"2" }}</a> </li>
{% endif %} <!-- Если существует страница текущая + 3 --> {% if page|add:"3" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"3" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"3" }}</a> </li>
{% endif %} <!-- Если нет страницы -3, но есть страница +4 --> {% if page|add:"-3" not in page_range and page|add:"4" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"4" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"4" }}</a> </li>
{% endif %} <!-- Если нет страницы -2, но есть страница +5 --> {% if page|add:"-2" not in page_range and page|add:"5" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"5" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"5" }}</a> </li>
{% endif %} <!-- Если нет страницы -1, но есть страница +6 --> {% if page|add:"-1" not in page_range and page|add:"6" in page_range %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ page|add:"6" }}&size={{ size }}&order={{ order }}&direction={{ direction }}">{{ page|add:"6" }}</a> </li>
{% endif %} <!-- Если существует следующая страница --> {% if paginator.has_next %}
<li class="page-item"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ paginator.next_page_number }}&size={{ size }}&order={{ order }}&direction={{ direction }}">»</a> </li>
{% else %}
<li class="page-item disabled"> <a class="page-link" href="#">»</a> </li>
{% endif %} <!-- Последняя страница -->
<li class="page-item {% if page == num_pages %}disabled{% endif %}"> <a class="page-link" href="/mytestblog/publication/?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page={{ num_pages }}&size={{ size }}&order={{ order }}&direction={{ direction }}">Последняя</a> </li>
</ul>
</nav>
{% endif %}
{% block сontent %}
<nowrap id="contentBlock">
{% include "pubcontent.html" %}
</nowrap>
<div class="col-md-12">
<div class="alert alert-info">
<div class="clearfix">Загрузка публикаций ...
<div class="spinner-border float-right" role="status">
<span class="sr-only">Загрузка публикаций ...</span>
</div>
</div>
</div>
</div>
<script type="text/javascript">
window.need_load_more=1;
window.need_page=2;
window.addEventListener('scroll', function() {
let windowRelativeBottom = document.documentElement.getBoundingClientRect().bottom;
if (windowRelativeBottom < document.documentElement.clientHeight + 100) {
if (window.need_load_more == 1) {
window.need_load_more = 0;
var myDiv = document.getElementById("contentBlock");
$.ajax({
url: "/mytestblog/pubcontent?cat={{ cat_id_join }}{% if tag.0 != "" %}&tag={{ tagstr_param }}{% endif %}&page=" + window.need_page + "&size={{ size }}&order={{ order }}&direction={{ direction }}",
success: function(data){
myDiv.insertAdjacentHTML('beforeEnd', data);
window.need_page = window.need_page + 1;
window.need_load_more = 1;
}
});
}; }
});
</script>
{% endblock %}