简体   繁体   中英

how to link to search results in pagination Django 3.1

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM