0

I want to implement pagination on the search results page of my site.

My Django project has a few apps that have different models and the search will look in every table for results.

for ex.:

# views.py
def search(request):
    queryset_list_ip_sensor = Ip_sensor.objects.all()  
    queryset_list = ControlValves.objects.all()  
    queryset_list_water_quality_sensor = Water_quality_sensor.objects.all()  
    context = {
            'ip_sensors': result_list_ip_sensor,
            'controlvalves': result_list_control_valves_2,
            'water_quality_sensor': result_list_water_quality_sensor,
            
            'values': request.GET, 
            'keywords': keywords
        }
    
    return render(request, 'pages/search.html', context)

I implemented pagination like this:

# views.py
def search(request): 

# ...

result = (result_list_ip_sensor,
          result_list_control_valves,
          result_list_water_quality_sensor)

paginator = Paginator(result, 1)  
page = request.GET.get('page')
paged_queries = paginator.get_page(page)

context_pagination = {'ip_sensors': result_list_ip_sensor,
                      'controlvalves': result_list_control_valves_2,
                      'water_quality_sensor': result_list_water_quality_sensor,
            
                      'queries': paged_queries,
                      'keywords': keywords,
                      'values': request.GET
                      }

return render(request, 'pages/search.html', context_pagination)
  • Before pagination I used to show results like this:

      {% if ip_sensors %}
          {% for ip_sensor in ip_sensors %}
              <div class="col-12 col-sm-12 col-md-4 col-lg-4 col-xl-4 mb-4">
                      <div class="card">
    
                          <div class="card-header">
                              I/P Sensor
                          </div>
                          <img class="card-img-top" src="{{ ip_sensor.cover.url }}" alt="">
                          <div class="card-body">
                              <div class="text-center">
                                  <h4 class="text-dark">{{  ip_sensor.title }}</h4>
                                  <p> {{  ip_sensor.description | truncatewords:10 }}</p>
                              </div>
                              <hr>
    
                              <div class="row py-2 text-dark">
                                <div class="col-6">
                                    Product Name: {{  ip_sensor.product_name | truncatewords:2 }}</div>
                                <div class="col-6">
                                    Usage: {{  ip_sensor.usage | truncatewords:4}}</div>
                              </div>
                              <hr>
    
    
                              <a href="{% url 'ip_sensor_item' ip_sensor.title %}" class="btn btn-light btn-block">More Info</a>
                          </div>
                      </div>
              </div>
          {% endfor %}
      {% endif %} 
    
    
      {% if controlvalves %}
          {% for controlvalve in controlvalves %}
              <div class="col-12 col-sm-12 col-md-4 col-lg-4 col-xl-4 mb-4">
                      <div class="card">
    
                          <div class="card-header">
                              Control Valves
                          </div>
    
                          <img class="card-img-top" src="{{ controlvalve.cover.url }}" alt="">
                          <div class="card-body">
                              <div class="text-center">
                                  <h4 class="text-dark">{{  controlvalve.title }}</h4>
                                  <p> {{  controlvalve.description | truncatewords:10 }}</p>
                              </div>
                              <hr>
    
                              <div class="row py-2 text-dark">
                                <div class="col-6">
                                    Product Name: {{  controlvalve.product_name | truncatewords:2 }}</div>
                                <div class="col-6">
                                    Usage: {{  controlvalve.usage | truncatewords:4}}</div>
                              </div>
                              <hr>
    
    
                              <a href="{% url 'valve' controlvalve.title %}" class="btn btn-light btn-block">More Info</a>
    
                          </div>
                      </div>
              </div>
          {% endfor %}
      {% endif %} 
    
      {% if water_quality_sensor %}
          {% for w_q_sensor in water_quality_sensor %}
              <div class="col-12 col-sm-12 col-md-4 col-lg-4 col-xl-4 mb-4">
                      <div class="card">
    
                          <div class="card-header">
                              Water Quality Sensor<br>
                          </div>
    
                          <img class="card-img-top" src="{{ w_q_sensor.cover.url }}" alt="">
                          <div class="card-body">
                              <div class="text-center">
                                  <h4 class="text-dark">{{  w_q_sensor.title }}</h4>
                                  <p> {{  w_q_sensor.description | truncatewords:10 }}</p>
                              </div>
                              <hr>
    
                              <div class="row py-2 text-dark">
                                <div class="col-6">
                                    Product Name: {{  w_q_sensor.product_name | truncatewords:2 }}</div>
                                <div class="col-6">
                                    Usage: {{  w_q_sensor.usage | truncatewords:4}}</div>
                              </div>
                              <hr>
    
                              <a href="{% url 'water_sensor_item' w_q_sensor.title %}" class="btn btn-light btn-block">More Info</a>
    
                          </div>
                      </div>
              </div>
          {% endfor %}
      {% endif %}  
    
  • The search result may contain an item from any table (model).

  • Each item has its own page and URL which should have a link in search result page

how to show results and define a link for them?

Shahriar.M
  • 818
  • 1
  • 11
  • 24

1 Answers1

0

The Paginator is expecting only one queryset from one model. You have 3 results sets, and therefore you will need 3 paginations.

Would you be able to split this view into 3 different views?

If your requirements don't allow this, look into using js to paginate the 3 different datasets in the same html template. I would use a frontend framework to fetch json from the django backend, and display 3 areas of the page independently.

Michael Lindsay
  • 1,290
  • 1
  • 8
  • 6
  • what do you mean by splitting the result page into 3 views? BTW, this was a simplified example, I have about 20 tables and 20 query_sets. Can you please give an example of how to use js to paginate between slices in one template? – Shahriar.M Nov 10 '20 at 10:10
  • 1
    One can only paginate one model's queryset per paginator. Sounds like you would need to have a list of all the Models/querysets, and then go into each models paginated querysets. I would definitely look into a frontend js library like vue.js. This would allow you to display the 20 different querysets each in its own dynamic div. For this soluution, I would use django rest framework to server json versions of the querysets. – Michael Lindsay Nov 10 '20 at 19:04