Django个性化分页

发布时间:2020-07-04 02:14:44 作者:yxuqtr
来源:网络 阅读:686

直接上代码吧:


# views.py
def accesslog(request):
    state = login_status(request.user.is_authenticated())
    log_list = AccessLog.objects.all().order_by()
    paginator = Paginator(log_list, 10)
    current_page_num = request.GET.get('page', 1)
    try:
        current_page = paginator.page(current_page_num)
    except EmptyPage:
        current_page = paginator.page(paginator.num_pages)
    except PageNotAnInteger:
        current_page = paginator.page(1)

    return render(request, 'radius/accesslog.html', {
            'current_page': current_page,
            'paginator': paginator,
            'state': state
    })
    
# templatetags/mytag.py

from django import template
register = template.Library()

@register.filter(name='substra')
def substra(value, arg):
   try:
      return int(value) - int(arg)
   except:
      return ''

HTML

# accesslog.html
{% load mytag %}
    <div class="container">
        <table class="table table-condensed">
            {% for log in current_page.object_list %}
                <tr>
                    <td>{{ log.id }}</td>
                    <td>{{ log.username }}</td>
                    <td>{{ log.ipaddr }}</td>
                    <td>{{ log.access_datetime|date:"Y-m-d H:s" }}</td>
                    {% if log.status %}
                        <td><span class="label label-success">认证成功</span></td>
                    {% else %}
                        <td><span class="label label-danger">认证失败</span></td>
                    {% endif %}
                    <td>{{ log.message }}</td>
                </tr>
            {% endfor %}

        </table>

    <nav>
{#    {{ current_page.paginator.num_pages }}#}
        <ul class="pagination">
            {% if current_page.number > 3 %}
                <li ><a href="?page=1">第一页</a></li>
            {% endif %}

            {% if current_page.number == 1 %}
                <li><a href="?page={{ current_page.number }}">1</a></li>
            {% endif %}

            {% if current_page.number > 2 %}
                <li><a href="?page={{ current_page.number|substra:2 }}">{{ current_page.number|substra:2 }}</a></li>
                <li><a href="?page={{ current_page.previous_page_number }}">{{ current_page.previous_page_number }}</a></li>
                <li><a href="?page={{ current_page.number }}">{{ current_page.number }}</a></li>
            {% elif current_page.number == 2 %}
                <li><a href="?page={{ current_page.previous_page_number }}">{{ current_page.previous_page_number }}</a></li>
                <li><a href="?page={{ current_page.number }}">{{ current_page.number }}</a></li>
            {% endif %}

            {% if current_page.number|add:2 <= current_page.paginator.num_pages %}
                <li><a href="?page={{ current_page.next_page_number }}">{{ current_page.next_page_number }}</a></li>
                <li><a href="?page={{ current_page.number|add:2 }}">{{ current_page.number|add:2 }}</a></li>
                <li><a href="?page={{ current_page.paginator.num_pages }}">最后一页</a></li>
            {% elif current_page.number == current_page.paginator.num_pages %}
            {# nothing to do#}
            {% elif current_page.number|add:2 >= current_page.paginator.num_pages %}
                <li><a href="?page={{ current_page.next_page_number }}">{{ current_page.next_page_number }}</a></li>
            {% endif %}



        </ul>
    </nav>

    </div>
    </div>
{% include 'radius/footer.html' %}

<script>
    $(document).ready(function(){
        $('.pagination li a').each(function(){
           if ( $(this).html() == {{ current_page.number }} ){
               $(this).parent().addClass('active')
           }
        });
    })
</script>


推荐阅读:
  1. Django 分页
  2. Django html 分页

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

django pagination paginator

上一篇:SQL语句基本语法

下一篇:windows环境测试IP地址是否通

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》