参考官方文档:
# 对象的通用视图class StudentListView(ListView): setion = '学生列表' model = Student # 获得数据库 template_name = 'teacher/student_list.html' # 重定义 context_object_name = 'students' # 数据重名字,没有HTML这个用object_list获取 # paginate_by = 10 def get_context_data(self,**kwargs): context = super().get_context_data(**kwargs) # 继承父类 context['setion'] = self.setion # 上下文管理 context['q'] = self.request.GET.get('q') context['total_sum'] = context['page_obj'].paginator.count # 获得总页数 context['per_page'] = self.request.GET.get('per_page',10) context['page'] = self.request.GET.get('page',1) return context def get_queryset(self): # 动态加载 student = Student.objects.filter(is_delete=False) # 定义过滤的对象列表 q = self.request.GET.get('q') per_page = self.request.GET.get('per_page',10) self.paginate_by = per_page if q: if q.isdigit(): student = Student.objects.filter(phone=q) else: student = Student.objects.filter(name=q) return student