Django ListView配置分页后无page_obj且分页失效问题求助
Hey, let's break down why your pagination isn't kicking in and how to fix it.
The Core Problem
You’ve defined a custom get_clients_view method in your Clients ListView subclass, and you’re returning a direct render response from it. This completely bypasses ListView’s built-in lifecycle—including its pagination logic. That’s why is_paginated is always False: none of the pagination context variables (is_paginated, page_obj, paginator) are being injected into your template context.
The Fix: Use ListView’s Built-in Hooks
Instead of overriding the entire view flow, use ListView’s intended methods to handle filtering and add extra context data. Here’s how to rewrite your view correctly:
Updated View Code
class Clients(ListView): paginate_by = 5 model = Client template_name = 'clients.html' context_object_name = 'object_list' # Matches your original context key def get_queryset(self): # Handle filtering here—this result will be automatically paginated queryset = super().get_queryset() last_name = self.request.GET.get('last_name', '') email = self.request.GET.get('email', '') if last_name: queryset = queryset.filter(last_name__contains=last_name) if email: queryset = queryset.filter(email__contains=email) return queryset def get_context_data(self, **kwargs): # Start with ListView's default context (includes pagination variables) context = super().get_context_data(**kwargs) # Add your forms to the context if self.request.method == 'GET': context['register_form'] = ClientRegisterForm() # Empty form for GET context['remove_form'] = ClientRemoveForm() context['search_form'] = ClientSearchForm(self.request.GET) # Populate with GET params context['update_form'] = ClientUpdateForm() else: # Handle POST form initialization if needed context['register_form'] = ClientRegisterForm(self.request.POST) context['remove_form'] = ClientRemoveForm(self.request.POST) context['update_form'] = ClientUpdateForm(self.request.POST) return context
Key Changes Explained
get_querysetinstead of custom filtering:
This method is where ListView expects you to modify the base queryset. The result here is automatically passed through ListView’s pagination system, which generates all the necessary pagination context variables.get_context_datafor extra context:
Use this hook to add your forms to the context without overriding ListView’s default behavior. The parentget_context_data()call ensures pagination variables are included.Fix form initialization:
In your original code, you passedrequest.POSTto forms during GET requests—this is incorrect. For GET requests, use empty forms or populate them withrequest.GET(like the search form).
Final Check: URL Configuration
Make sure your URL is pointing to the ListView’s as_view() method, not your old custom method:
path('clients/', Clients.as_view(), name='clients'),
With these changes, your template’s {% if is_paginated %} check will start working, and the pagination controls will behave as expected.
内容的提问来源于stack exchange,提问作者Dave Schuster

