【问题标题】:How to handle Django Pagination with Nested tabs如何使用嵌套选项卡处理 Django 分页
【发布时间】:2020-02-15 21:38:47
【问题描述】:

我想为我的模板添加分页,我的模板有嵌套选项卡(见代码),我的所有内部选项卡都包含一个表格,我想为我的内部选项卡中的所有表格添加分页。

到目前为止,我已经在我的一个表格中添加了分页并且它可以工作,但问题是每当页面刷新时,它都会转到我的第一个标签 <!-- overview-->,而不是发出请求的那个,尽管分页正在工作。

这是我的标签的结构。我没有使用自定义 JS 或 CSS,只有 BS4 类

<div class="tab-content" id="nav-tabContent">
   <!--OVERVIEW TAB-->
   <!--STATIC TAB-->
       <!--TAB 1-->
       <!--TAB 2-->
   <!-- FILES TAB-->
       <!--TAB 1 -->
       <!--TAB 2 -->
       <!--TAB 3 -->
   <!--Registry TAB -->
       <!--TAB 1 -->
       <!--TAB 2 -->
   <!--Network TAB-->
       <!--TAB 1 -->
       <!--TAB 2 -->
       <!--TAB 3 -->
</div>

TEMPLATE.html

<nav aria-label="Page navigation">
           {% if strings.has_other_pages %}
           <ul class="pagination justify-content-center pagination-lg">
              {% if strings.has_previous %}
              <li class="page-item"><a class="page-link" href="?page=1">First</a> </li>
              {% endif %}

              {% for i in strings.paginator.page_range %}
                 {% if strings.number == i %}
             <li class="page-item active"><a class="page-link" href="{{ i }}">{{ i }}</a></li>
                 {% elif i > strings.number|add:'-3' and i < strings.number|add:'3' %}
             <li><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
                 {% endif %}
              {% endfor %}

              {% if strings.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ strings.paginator.num_pages }}">Last</a> </li>
              {% endif %}
            </ul>
            {% endif %}
</nav>

views.py

def viewable(request):
    if request.method == 'GET':
        page = request.GET.get('page', 1)

        strings = [ 'a','b','c'] #There are about 2000 entries in this list

 #STRING PAGE PAGINATOR
        stringspaginator = Paginator(strings,100)
        try:
            strings = stringspaginator.page(page)
        except PageNotAnInteger:
            strings = stringspaginator.page(1)
        except EmptyPage:
            strings = stringspaginator.page(stringspaginator.num_pages)

         return render(request, 'viewable.html', {"metadata" : metadata,
                                                 "folders_created": folders_created,
                                                 "files_created": files_created ,
                                                 "files_deleted":files_deleted,
                                                "registry_written":registry_written,
                                                 "registry_deleted": registry_deleted,
                                                 "http":http,
                                                 "tcp":tcp,
                                                 "udp":udp,
                                                 'positives': positives,
                                                 'negatives': negatives,
                                                 'libs': libs,
                                                 'strings': strings
                                                 })

现在我的理解是我应该在发出请求的 GET 请求中添加选项卡,并基于它应该重定向到正确的选项卡,但我不知道如何。或者即使这是正确的解决方案。

我正在向我的模板发送大量变量,然后显示在多个表格中,我认为这不是处理这么多数据的最佳解决方案,但我是初学者,现在这是唯一的方法我知道。

【问题讨论】:

    标签: python html django pagination


    【解决方案1】:

    因此,由于我没有收到任何响应,并且它认为使用服务器可能难以处理,因此我使用DataTables JS 解决方案。它快速高效,并且有许多用于表格定制的绝佳选择。所以我建议这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 2020-07-05
      • 1970-01-01
      相关资源
      最近更新 更多