Django: Pagination and search example

#--- Django controller called views in Django --- #

# Create your views here.
# Following class is using the template blog/templates/blog/post_list.html
class PostList(ListView):
    paginate_by = 2 #this variable is used for pagination 
    def get_queryset(self):
                
        if 'search' in self.request.GET:
            objects = Post.objects.filter(
                Q(title__icontains=self.request.GET['search']) | Q(body__icontains=self.request.GET['search'])
                )
        else:
            objects = Post.objects.all()
        
        return objects


#Template

{% load blog_extras %} 
{% for post in page_obj %}
    {% show_post post %}
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if page_obj.has_previous %}
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
        </span>

        {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>