【问题标题】:How to use pagination with modal view in django如何在 django 中使用带有模态视图的分页
【发布时间】:2021-08-03 04:12:45
【问题描述】:

我在 django 的模态视图中显示带有分页的 ListView,但是当我单击下一页的链接时,它会关闭模态。我想在该模式中显示下一个结果页面。或者关闭该模态并在新的模态视图中打开一个新的模态视图

如何在该模式中使用分页(显示下一个结果页面)

这是模态

<div class="modal-dialog modal-dialog-centered modal-lg" >
  <div class="modal-content">
  <div class="wrapper wrapper-content {% block entry %} animated fadeInRight {% endblock entry %}">
            <div class="row">
                <div class="col-lg-12">

      <h3>Trabajadores: {{ list_title }}</h3>

    <div class="col-12">
        <div class="">
        </div>
    </div>
     <div class="table-responsive">
    <table class="table table-bordered table-hover" id="mydatatable">
        <thead>
            <tr>
               
                <th class="text-navy" >Nombre</th>
                <th class="text-navy">Primer apellido</th>
                <th class="text-navy">Cédula</th>
                <th class="text-navy">Email</th>
            </tr>
        </thead>
        <tbody>

        {% for worker in workers %}
<tr class="clickable" onclick="window.location='{% url 'riesgo:worker_detail' worker.id %}'">
                <td>
                   
                    {{ worker.name }}
                </td>
                <td >
                    {{ worker.last_name1 }}
                </td>
                <td >
                    {{ worker.identification }}
                </td>
                <td >
                    {{ worker.email }}
                </td>
            </tr>
         </tbody>
        {% empty %}
            <span style="color: red">{{ list_message }}</span>
        {% endfor %}
    </table>
    </div>
    <div class="pull-right">
        <nav>
            <ul class="pagination">
                
                {% if 'name' in request.get_full_path %}
                    {% if has_previous_pages %}
                        <li class="page-item">
                            <a class="page-link" href="{{ request.get_full_path }}&page={{ 1 }}" aria-label="1...">
                                <span aria-hidden="true">Página 1</span>
                                <span class="sr-only">1...</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="{{ request.get_full_path }}&page={{ previous_page }}" aria-label="Anterior">
                                <span aria-hidden="true">&laquo;</span>
                                <span class="sr-only">Anterior</span>
                            </a>
                        </li>
                    {% endif %}
                    {% for pr in paginator_rows %}
                        {% if pr == actual_page %}
                            <li class="page-item active"><a class="page-link" href="{{ request.get_full_path }}&page={{ pr }}">{{ pr }}</a></li>
                        {% else %}
                            <li class="page-item"><a class="page-link" href="{{ request.get_full_path }}&page={{ pr }}">{{ pr }}</a></li>
                        
                        {% endif %}
                    {% endfor %}
                    {% if has_more_pages %}
                        <li class="page-item">
                            <a class="page-link" href="{{ request.get_full_path }}&page={{ next_page }}" aria-label="Siguiente">
                                <span aria-hidden="true">&raquo;</span>
                                <span class="sr-only">Siguiente</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="{{ request.get_full_path }}&page={{ total_pages }}" aria-label="1...">
                                <span aria-hidden="true">Página {{ total_pages }}</span>
                                <span class="sr-only">{{ total_pages }}...</span>
                            </a>
                        </li>
                    {% endif %}
                
                {% else %}
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ 1 }}" aria-label="1...">
                                <span aria-hidden="true">Página 1</span>
                                <span class="sr-only">1...</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Anterior">
                                <span aria-hidden="true">&laquo;</span>
                                <span class="sr-only">Anterior</span>
                            </a>
                        </li>
                    {% endif %}
                    {% for pr in paginator_rows %}
                        {% if pr == page_obj.number %}
                            <li class="page-item active"><a class="page-link" href="?page={{ pr }}">{{ pr }}</a></li>
                        {% else %}
                            <li class="page-item"><a class="page-link" href="?page={{ pr }}">{{ pr }}</a></li>
                        {% endif %}

                    {% endfor %}
                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Siguiente">
                                <span aria-hidden="true">&raquo;</span>
                                <span class="sr-only">Siguiente</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}" aria-label="1...">
                                <span aria-hidden="true">Página {{ page_obj.paginator.num_pages }}</span>
                                <span class="sr-only">{{ page_obj.paginator.num_pages }}...</span>
                            </a>
                        </li>
                    {% endif %}
                {% endif %}
            </ul>
        </nav>
    </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" onclick="return close_modal()">Cerrar</button>

            <a href="{% url 'riesgo:worker_create'%}" class="btn btn-primary"><i class="fa fa-plus"></i>&nbsp;&nbsp;Crear</a>
                </div>
  </div>
</div>
                </div>
  </div>
</div>

这是列表视图:

class WorkerModalListView(LoginRequiredMixin, ListView):
    model = WorkerModel
    group_required = [u'Auxiliar Legal', 'Jefe de la Oficina Local', 'Jefe de la RBRP']
    template_name = 'riesgo/worker/worker_list_modal.html'
    context_object_name = 'workers'
    paginate_by = 10

    def get_context_data(self, *args, **kwargs):
        context = super(WorkerModalListView, self).get_context_data(*args, **kwargs)
        #context['name'] = self.request.GET.get('name', '')
        context['value_name'] = self.request.GET.get('name', '')
        #print(context['name'])

        self.request.session['page_from'] = "persona"
        self.request.session['referer'] = ""

        if context['is_paginated']:
            list_pages = []

            if 'name' not in self.request.GET:
                for i in range(context['page_obj'].number, context['page_obj'].number + 5):
                    if i <= context['page_obj'].paginator.num_pages:
                        list_pages.append(i)
            else:
                first_range = self.request.GET.get('page', '1')

                if len(WorkerListView.get_queryset(self)) % self.paginate_by == 0:
                    paginated = int(len(WorkerModalListView.get_queryset(self)) / self.paginate_by)
                else:
                    paginated = int(len(WorkerModalListView.get_queryset(self)) / self.paginate_by) + 1

                if paginated > 1:
                    for i in range(int(first_range), int(first_range) + 5):
                        if i <= paginated:
                            list_pages.append(i)

                    context['total_pages'] = paginated
                    context['has_more_pages'] = True if int(first_range) < paginated else False
                    context['next_page'] = int(first_range) + 1 if int(first_range) < paginated else '0'
                    context['has_previous_pages'] = True if int(first_range) > 1 else False
                    context['previous_page'] = int(first_range) - 1 if int(first_range) > 1 else '0'
                    context['actual_page'] = int(first_range)

            context['paginator_rows'] = list_pages

        return context

【问题讨论】:

    标签: javascript python django bootstrap-4


    【解决方案1】:

    通常我会在需要分页时插入一段代码,如下所示:

    <nav>
    <ul class="pagination justify-content-center">
        {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page=1">&laquo; first</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}">previous</a>
            </li>
        {% endif %}
    
        <li class="page-item active">
            <a  class="page-link">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</a>
        </li>
    
        {% if page_obj.has_next %}
        <li class="page-item">
            <a class="page-link" href="?page={{ page_obj.next_page_number }}">next</a>
        </li>
        <li class="page-item">
            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
        </li>
        {% endif %}
    </ul>
    </nav>
    

    在我将它插入到我需要的页面上之后:

    {% include 'components/paginate_index.html' %}
    

    对于我认为你工作太多的视图,常规列表视图将包含分页的所有项目,因此普通列表视图应如下所示:

    class AccountsListView(LoginRequiredMixin, ListView):
        model = User
        paginate_by = 15
        template_name = "accounts/employees/listView.html"
    

    【讨论】:

    • 希望对您有所帮助。
    • 谢谢,但问题是我想在模态视图中使用分页,单击任何链接都会关闭模态。数据表可以提供帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2021-11-19
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多