首页 > 代码库 > Django个性化分页
Django个性化分页
直接上代码吧:
# 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="http://www.mamicode.com/?page=1">第一页</a></li> {% endif %} {% if current_page.number == 1 %} <li><a href="http://www.mamicode.com/?page={{ current_page.number }}">1</a></li> {% endif %} {% if current_page.number > 2 %} <li><a href="http://www.mamicode.com/?page={{ current_page.number|substra:2 }}">{{ current_page.number|substra:2 }}</a></li> <li><a href="http://www.mamicode.com/?page={{ current_page.previous_page_number }}">{{ current_page.previous_page_number }}</a></li> <li><a href="http://www.mamicode.com/?page={{ current_page.number }}">{{ current_page.number }}</a></li> {% elif current_page.number == 2 %} <li><a href="http://www.mamicode.com/?page={{ current_page.previous_page_number }}">{{ current_page.previous_page_number }}</a></li> <li><a href="http://www.mamicode.com/?page={{ current_page.number }}">{{ current_page.number }}</a></li> {% endif %} {% if current_page.number|add:2 <= current_page.paginator.num_pages %} <li><a href="http://www.mamicode.com/?page={{ current_page.next_page_number }}">{{ current_page.next_page_number }}</a></li> <li><a href="http://www.mamicode.com/?page={{ current_page.number|add:2 }}">{{ current_page.number|add:2 }}</a></li> <li><a href="http://www.mamicode.com/?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="http://www.mamicode.com/?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>
本文出自 “jun.wang” 博客,请务必保留此出处http://junwang.blog.51cto.com/5050337/1876819
Django个性化分页
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。