How to add pagination to search view in django, I tried the below

Islam Fahmy

could you please support adding pagination to the below search function based view, I tried the below but it gives me all the results in one page also when I press on next it return me back to the empty search form page

This my function

def post_search(request):
form = SearchForm()
query = None
results = []
if 'query' in request.GET:
    form = SearchForm(request.GET)
    if form.is_valid():
        query = form.cleaned_data['query']
        search_vector = SearchVector('title', weight='A') + \
                        SearchVector('body', weight='B')
        search_query = SearchQuery(query)
        results = Post.published.annotate(
                    search=search_vector,
                    rank=SearchRank(search_vector, search_query)
                ).filter(rank__gte=0.3).order_by('-rank')

paginator = Paginator(results, 2) # 3 posts in each page
page = request.GET.get('page')
try:
    posts = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer deliver the first page
    posts = paginator.page(1)
except EmptyPage:
    # If page is out of range deliver last page of results
    posts = paginator.page(paginator.num_pages)

return render(request,
              'blog/post/search.html',
              {'form': form,
              'page': page,
              'posts': posts,
              'query': query,
              'results': results})

And here is pagination.html file

<div class="pagination">
<span class="step-links">
  {% if page.has_previous %}
    <a href="?page={{ page.previous_page_number }}">Previous</a>
  {% endif %}
  <span class="current">
    Page {{ page.number }} of {{ page.paginator.num_pages }}.
  </span>
  {% if page.has_next %}
    <a href="?page={{ page.next_page_number }}">Next</a>
  {% endif %}
</span>
and I included it in the search.html file using the below
{% include "pagination.html" with page=posts %} 
Antoine Pinsard

You have to use Django's Paginator

def post_search(request):
    form = SearchForm()
    query = None
    results = []
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            query = form.cleaned_data['query']
            search_vector = SearchVector('title', weight='A') + \
                            SearchVector('body', weight='B')
            search_query = SearchQuery(query)
            results = Post.published.annotate(
                        search=search_vector,
                        rank=SearchRank(search_vector, search_query)
                    ).filter(rank__gte=0.1).order_by('-rank')
            paginator = Paginator(results, 25) # Show 25 results per page.
            page_number = request.GET.get('page')
            page_obj = paginator.get_page(page_number)

    return render(request,
                  'blog/post/search.html',
              {'form': form,
              'query': query,
              'page_obj': page_obj})

You will also have to adjust your template as described in the linked documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add pagination in Restangular and Django Rest Framework?

Android ConstraintLayout: How to add a dynamic view one below another

How to add pagination in a list on a frontend view?

How to add space below scroll view

How to add pagination in php image grid view?

How to add a view to WindowManager that appears below AlertDialog

how to add child view exact below its parent firebase android

How to add a line below Navigation bar in the pushed View Controller?

How can I add pagination for comments?

How to add pagination in search.html - django

How do I add Pagination to a table (Django)

How can I add a search function the data below (Ideally in another component)

How do i use pagination in django

Add a view below actionbarsherlock

How does django's pagination work in a django view

How to add the View below the TableView?

Android: How to add Image and then text below it in scroll view?

How to add many fields to search button for the below query-wordpress

How to add horizontal scroll view below a listview

How to add pagination in search results

How to search a String and Add new String below the result?

How to add pagination in Classed Based View with get_context_data

How can i give pagination to Following View

How can I add pagination to search result

How to add pagination on my search results page?

How can I create view as below

How I can add pagination in swift?

How to add buttons below our site in the results of a search?

I want to add background audio in my splash screen. So tried the below code. But I got some exceptions. ( Flutter )