有没有办法通过各个模板元素进行分页?

时间:2019-03-08 18:54:10

标签: django django-templates django-views

问题:

我目前有一个模板,其中包含多个嵌套的div,其中将包含不同的列表项。当前,如果我在其中一个元素上单击“下一步”按钮,则会重新加载整个页面,现在将重新加载所有元素,或者在下一页或上一页上。

代码:

div中的主模板代码

      {% include "users/activities.html" with activities=action1 table_title='Signups' action_verb='action1' %}
      {% include "users/activities.html" with activities=action2 table_title='Actions' %}
      {% include "users/activities.html" with activities=action3 table_title='Actions 2'%}

带有分页的活动模板代码:

    {% if activities.has_other_pages %}
    <div class="paginationWrap">
      <ul class="pagination">
        {% if activities.has_previous %}
          <li><a href="?page={{ activities.previous_page_number }}">&laquo;</a></li>
        {% else %}
          <li class="disabled"><span>&laquo;</span></li>
        {% endif %}
        {% for i in activities.paginator.page_range %}
          {% if activities.number == i %}
            <li class="active"><span> {{ i }} </span></li>
          {% else %}
            <li><a href="?page={{ i }}">{{ i }}</a></li>
          {% endif %}
        {% endfor %}
        {% if activities.has_next %}
          <li><a href="?page={{ activities.next_page_number }}">&raquo;</a></li>
        {% else %}
          <li class="disabled"><span>&raquo;</span></li>
        {% endif %}
      </ul>
    </div>
    {% endif %}

目标:

单击一个div并不能完全包含所有div的功能可以返回页面更改。

1 个答案:

答案 0 :(得分:0)

如果您不想加载整个页面,请开发带有分页选项的api,然后使用ajax请求将其选中,以使用ajax请求更新数据。

#python
class ActivitiesView(APIView):
    def get(self, request, format=None):
        activity_details = Activities.objects.filter(user=request.user) # or any other query
        paginator = Paginator()
        response = paginator.generate_response(activity_details, ActivitySerializer, request)
        return response


   #js
       $.ajax({
            type: "GET",
            url: "{% url ur_url %}",
            data: "page="+pageNum,
            async: false,
            dataType: 'json',
            success: function(data){
                el = $('#friends-data');
                update_div(data.json);
            }
        });

您也可以DRF PAGINATION

进行检查

检查此页还讨论using django views