Reputation: 37904
I am trying to understand the principles of Django pagination logic.
My understanding problem is:
I have a search result page with 40 items inside. I want to use pagifier and show only 10 items per page.
Now let's say, someone has searched for "car BMW" in my search. then he lands to first page with 10 cars.
My question is, whether the query "car BMW" will be kept in request?
I am not quite sure about the view which is paging the pages.
Do I have to have 2 view functions:
Can someone please help me understand this logic? I read the docs but it is not mentioned there or I am too dumb to understand it.
Upvotes: 0
Views: 148
Reputation: 15231
Pagination is only concerned with queryset and just take your queryset and slices it to return the number of entries you expect on a particular page. eg: In your case, only 10 entries applicable for a particular page.
It doesn't make any modification to your request.
You can handle it with a single view.
Example:
def get_cars(self, request, page=1):
cars_per_page = 10
search_term = request.REQUEST.get('car_type') #car_type 'car bmw' will still be kept in request
all_cars = Car.objects.filter(car_type__contains=search_term)
paginator = Paginator(all_cars, cars_per_page)
page_ = paginator.page(page)
result_cars = page_.object_list
return render(request, "search_cars.html", {'search_term': search_term})
If you expect page
to be available in request as well, then you can do:
def get_cars(self, request):
page = request.GET.get('page')
...
#Everything else described above.
...
Upvotes: 1
Reputation: 12054
Usually, search request will be:
example.com/search/?q=car+bmw
Pagination will use query (to filter objects by car+bmw) and then apply limit. For first page it will be (0, 10) When somebody will click at second page, the request will be:
example.com/search/?q=car+bmw&page=2
Pagination will use the same query but with another offset: (10, 20)
You just need to provide the desired query to pagination, and pagination will do the rest of the work.
From django example, you need to modify contact_list
query.
Upvotes: 3