Django分页前后端处理

1.view处理

view.py

from django.core.paginator import Paginator
def book_list()
    page = request.GET.get('page')# 页码
    limit = request.GET.get('limit')# 每页n条
    questset = BookInfo.objects.all().order_by('-create_time')
    lens = len(questset)
    pages = Paginator(questset, int(limit))
    if page:
        questset = pages.page(int(page))
    else:
        questset = pages.page(1)
    detail = []
    for book in BookInfo:
        bk = {
            'name':book.name,
            'username':book.account.name,
            'datatime':book.create_time
        }
        detail.append(bk)
    context = {
        'data':detail,
        'len':lens,
        'pages_list':list(range(a,int(lens/limit)+1)),
        'limit':limit,
    }
    return render(request, 'list.html', context=context)
2.jinja模板

list.html

{% for bk in detail %}
          <table>
            <tr>
              <td style="width: 70%">{{ bk.name }}td>
              <td style="width: 10%">{{ bk.username }}td>
              <td style="width: 20%">{{bk.datatime }}td>
            tr>
          table>
          <hr>
{% endfor %}
<div style="padding: 1px 30px">
    <span class="layui-laypage-count">共 {{ len }} 条span>
    {% for i in pages_list%}
      <a href="/?page={{i}}" data-page="{{ i }}" onclick="btnOnclick({{ i }})">{{i}}a>
    {% endfor %}
    <span class="layui-laypage-count">{{limit}}条/页span>
div>

你可能感兴趣的:(前端开发,后端人生,云计算)