Paginator 简单分页

2022-08-05 18:30:43 浏览数 (1)

代码语言:javascript复制
#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())#返回页面
代码语言:javascript复制
<!-- 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 %}

0 人点赞