Paginator 简单分页

By 肖奈
2017-11-20
5497 read
#views.py
def login_log(req):   
    try:
        winlog_list = login_log.objects.filter(username=username).order_by('-logintime')#数据库信息
        after_range_num = 5
        before_range_num = 4#前4后4
        page=int(req.GET.get('page','1'))#获取page,默认为1
    except Exception as e:
          print(e)
          raise Http404
    try:
        if page < 1:
            page=1#小于1为1
    except ValueError:
        page=1
    paginator = Paginator(winlog_list, 10)#分页,每页十个
    try:
        winloglist = paginator.page(page)#获取第几页信息
    except (EmptyPage,InvalidPage,PageNotAnInteger):
        winloglist = paginator.page(1)
    if page >= after_range_num:
        page_range = paginator.page_range[page-after_range_num:page+before_range_num]
    else:       
        page_range = paginator.page_range[0:int(page)+before_range_num]
    return render_to_response('user/log.html',locals())#返回页面




<!-- log.html -->
{% for post in winloglist %}
        <tr>
            <td>
                {{post.logintime}}
            </td>
            <td>
               {{post.ip_add}}
            </td>
        </tr>
{% endfor %}
       
    </tbody>
    <tfoot>
     
</table>
  <div class="page black2">
        {% if winloglist.has_previous %}
        <a class="disabled" href="?page={{ winloglist.previous_page_number }}" title="上一页">
            上一页
        </a>
        {% endif %}
{% for p in page_range %}
{% ifequal p winloglist.number %}
        <span class="current">
            {{p}}
        </span>
        {% else %}
        <a href="?page={{p}}" title="第{{p}}页">
            {{p}}
        </a>
        {% endifequal %}
{% endfor %} 
{% if winloglist.has_next %}
        <a href="?page={{ winloglist.next_page_number }}" title="下一页">
            下一页
        </a>
        {% endif %}


python2 python3共存问题及pip2的安装
scapy 模块的安装

Comments

暂无评论,还不快来坐沙发...

Leave a Reply