Как сделать пагинацию нескольких запросов в одном представлении на основе функций или классов в django?
У меня есть функция поиска, которая запрашивает несколько моделей. Ожидаемые результаты отображаются в html-шаблоне, и пока все в порядке. Проблема в том, что я хочу выводить результаты постранично, используя встроенный в django класс Pagination. Пагинация с несколькими моделями - вот где я застрял. У меня есть другие представления, основанные на классах, которые хорошо работают с пагинацией одиночных моделей.
def search_results(request):
if request.method == 'POST':
searched = request.POST['searched']
books = Book.objects.filter(description__icontains=searched,
title__icontains=searched).order_by('-id')
sermons = Sermon.objects.filter(description__icontains=searched,
title__icontains=searched).order_by('-id')
other_sermons = SermonsByOtherFathers.objects.filter(description__icontains=searched,
title__icontains=searched).order_by('id')
other_books = BooksByOtherFathers.objects.filter(description__icontains=searched,
title__icontains=searched).order_by('-id')
context = {
'searched': searched,
'sermons': sermons,
'other_sermons': other_sermons,
'books': books,
'other_books': other_books,
}
if searched == "":
return HttpResponse('Please type something in the search input.')
return render(request, "search_results.html", context)
Это упрощенная версия моего html-шаблона.
{% for book in books %}
<tr>
<td>
<a href="{{ book.book_instance.url }}"> {{ book.title }} </a>
<p> {{book.author}} </div>
<p> {{ book.category }} </p>
<p> {{ book.description }} </p>
</td>
</tr>
{% endfor %}
<-- ...and the same loops goes for the rest of the other querysets. -->
{% for book in other_books %}
<-- code here -->
{% endfor %}
{% for sermon in sermons %}
<-- code here -->
{% endfor %}
{% for sermon in other_sermons %}
<-- code here -->
{% endfor %}
Любая помощь с частью пагинации django с несколькими моделями будет оценена по достоинству.