django分页(Django分页查询如何实现)

网友投稿 645 2022-09-06


django分页(Django分页查询如何实现)

​​border="1px">

ID

名称

操作

{% for cls in paginator_data.object_list %}

{{ cls.id }}

{{ cls.title }}

{% endfor %}

{% if paginator_data.has_previous %}

上一页

{% endif %}

{% for page in paginator_data.paginator.page_range %}

{% if paginator_data.number == page %}

{{ page }}

{% else %}

{{ page }}

{% endif %}

{% endfor %}

{% if paginator_data.has_next %}

下一页

{% endif %}

{% include 'modal_classes.html' %}

class del_classes(ListView): model = Classes paginate_by = 10 template_name= 'get_classes.html' def delete(self,request,*args,**kwargs): print(kwargs) pk = kwargs.get('pk') print(pk) Classes.objects.filter(id=pk).delete() return JsonResponse({'status':1}) def post(self,request,*args,**kwargs): data = QueryDict(request.body).dict() print(data) Classes.objects.create(**data) print(data) return JsonResponse({'status': 1}) def handle_page(self, page, object_list): paginator = Paginator(object_list, self.paginate_by, 1) try: paginator_data = paginator.page(page) except PageNotAnInteger: paginator_data = paginator.page(1) except EmptyPage: paginator_data = paginator.page(paginator.num_pages) return paginator_data def get(self, request, *args, **kwargs): pk = kwargs.get('pk') if pk: try: instance = self.model.objects.get(pk=pk) return render(request, 'get_classes.html', {'cls': instance}) #return render(request, self.template_detail, instance.to_dict) except self.model.DoesNotExist: return JsonResponse({'data':'id {} not exits'.format(pk)}) object_list = self.get_queryset() page = request.GET.get('page',1) paginator_data = self.handle_page(page, object_list) search = request.GET.get('search', '') return render(request, self.template_name, {'paginator_data': paginator_data, 'search': search})


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:机器学习基础:六种方法帮你解决模型过拟合问题(如何理解模型训练过拟合和欠拟合)
下一篇:ThreadLocal工作原理及用法案例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~