折腾了一番,终于把django中的ListView视图的分页效果做好了,话不多说,首先展示的是效果图:
相关代码如下:
views.js
class IndexView(ListView):
model = User
template_name = 'users/index.html'
paginate_by = 5 #每页显示多少条数据
ordering = 'username' #以username字段排序
context_object_name = 'users' # 传到template中的list
page_kwarg = 'page' # 获取第几页的数据的参数名称(?page=xxx)。默认是`page`
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
current_page = context.get('page_obj').number #当前页
total_page = context.get('paginator').num_pages #总共的页数
if current_page<=5:
pagerange = range(1,5)
elif 7<current_page< total_page and (current_page+2)