如何加载到Django Paginator Page(下一个/上一个)?

时间:2019-05-13 11:14:11

标签: django django-templates django-views

嗨, 我遵循了有关多模型搜索的教程,并且该方法有效。现在我想对输出进行分页。但是如何将分页页面(下一个/上一个)数据加载到模板中?到目前为止,如果我按下 next ,我会得到一个错误。

views.py

from django.views.generic import TemplateView, ListView
from django.views.generic import View
from django.shortcuts import render
from django.db.models import Q
from django.core.paginator import Paginator
from itertools import chain

# --- Import Models
from datainput.models import Animal
from datainput.models import Farmer

<....>

class SearchView(ListView):
    template_name = 'farmapi/searchview.html'
    paginate_by = 2
    count = 0

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['count'] = self.count or 0
        context['query'] = self.request.GET.get('q')
        return context

    def get_queryset(self):
        request = self.request
        query = request.GET.get('q', None)

        if query is not None:
            farmer_results        = Farmer.objects.search(query)
            animal_results      = Animal.objects.search(query)


        # combine querysets 
            queryset_chain = chain(
                    farmer_results,
                    animal_results
            )        
            qs = sorted(queryset_chain, 
                    key=lambda instance: instance.pk, 
                    reverse=True)
            self.count = len(qs) # since qs is actually a list
            return qs

        return Farmer.objects.none() # just an empty queryset as default

template.html

{% extends 'base.html' %}
{% load class_name %}
{% load static %}

{% block custom_css %}
<link rel="stylesheet" type="text/css" href="{% static 'css/home_styles.css' %}">
{% endblock %}

{% block content %}
<div style="height: 10px;">
</div>
<div class="container-fluid">
    <div class='row'>
        <div class="col-4 offset-md-8">
            <form method='GET' class='' action='.'>
                <div class="input-group form-group-no-border mx-auto" style="margin-bottom: 0px; font-size: 32px;">
                    <span class="input-group-addon cfe-nav" style='color:#000'>
                        <i class="fa fa-search" aria-hidden="true"></i>
                    </span>
                    <input type="text" name="q" data-toggle="popover" data-placement="bottom" data-content="Press enter to search" class="form-control cfe-nav mt-0 py-3" placeholder="Search..." value="" style="" data-original-title="" title="" autofocus="autofocus">
                </div>
            </form>
        </div>
    </div>
</div>

<div style="height: 10px;">
</div>

<div class="container-fluid">
    <div class="row">
        <div class="col-6 offset-md-4">
            {% for object in object_list %}
            {% with object|class_name as klass %}
            {% if klass == 'Farmer' %}
            <div class='row'>
                Farmer: <a href='{{ object.get_absolute_url }}'> {{ object.first_name }} {{ object.last_name }}</a>
            </div>
            {% elif klass == 'Animal' %}
            <div class='row'>
                Animal: <a href='{{ object.get_absolute_url }}'> {{ object.name }} {{ object.species }}</a>
            </div>
            {% else %}
            <div class='row'>
                <a href='{{ object.get_absolute_url }}'>{{ object }} | {{ object|class_name }}</a>
            </div>
            {% endif %}

            {% endwith %}

            {% empty %}

            {% endfor %}
            <div style="height: 10px;">
            </div>
            <div class='row'>
                <results>{{ count }} results for <b>{{ query }}</b></results>
            </div>
        </div>
    </div>
</div>

<div class="container-fluid">
    <!-- Pagniator Data -->
    <div class="paginator">
        <span class="step-links">

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

            {% if page_obj.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
            {% endif %}



            {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">next</a>
            <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
            {% endif %}

        </span>
    </div>

</div>
    {% endblock %}

如果我的模板向我显示了例如“ xyz的4个结果” “第1页,共2页” ,并且我按下了下一个最后页,然后我收到此错误消息:

  

找不到页面(404)请求方法:GET请求URL:     the wiki提出者:     farmapi.views.SearchView

     

无效页面(2):该页面不包含任何结果

据我了解,我必须显式分页查询集的返回值,以便分页器将其放入?还是我错过了请求分页器的请求?

1 个答案:

答案 0 :(得分:0)

我还使用了分页器,用户可以定义每页的项目数。我添加了以下方法:

def get_paginate_by(self, queryset):
    limit = int(self.request.POST.get('limit', 25))

    return limit

在模板中,它看起来像:

{% if is_paginated %}
     {{ page_obj|render_paginator }}
{% endif %}

其中render_paginator是模板标记。 在您的模板中调用一个网址,例如:your_url?page = 2