查询结果后如何使用分页?

时间:2019-06-17 08:06:03

标签: python django

我正在尝试使用2个参数进行搜索。一切正常,直到我想使用分页。 我一直在通过每页仅显示1个元素进行测试。在我要更改页面之前,此方法有效。

更改页面时,即使我有应在数据库中显示的条目也没有任何结果。

我想我应该以某种方式存储搜索参数并在页面更改时进行查询,但这对我来说毫无意义。如果每次更改页面时仍然必须执行查询,那么分页有什么帮助?

Views.py

def cautare(req):
    intrari_oferte = Oferta.objects.get_queryset().distinct('denumireMeserie')
    context = {
        'title': "Cautare loc de munca | Best DAVNIC73",
        'oferte': intrari_oferte
    }
    return render(req, "../templates/pagini/cautare-loc-de-munca.html", context)

def locuridemunca(req):
    jud = req.POST.get('inputjudet')
    meserie = req.POST.get('inputmeserie')
    intrari_oferte = Oferta.objects.filter(judet=jud, denumireMeserie=meserie)
    pagIntrari = Paginator(intrari_oferte, 1)
    page = req.GET.get('page')
    pagina = pagIntrari.get_page(page)
    context = {
        'title': "Cautare loc de munca | Best DAVNIC73",
        'judet': jud,
        'meserie': meserie,
        'pagina': pagina
    }
    return render(req, "../templates/pagini/locuri-de-munca.html", context)

cautare模板

{% extends 'base.html' %}
{% load static %}
{% block content %}
            <div class="container container-centru">
                <h1 class="heading-contact">Cauta loc de munca</h1>
                <form class="form-contact" action="{% url 'locuridemunca' %}" method="POST">
                  {% csrf_token %}
                    <select name="inputjudet" class="selectare-judet-cautare" style="display: block;">
                        <option selected disabled>Selecteaza judetul!</option>
                        <optgroup label="Sud Muntenia">
                            <option value="Prahova">Prahova</option>
                            <option value="Dambovita">Dambovita</option>
                            <option value="Calarasi">Calarasi</option>
                            <option value="Arges">Arges</option>
                            <option value="Teleorman">Teleorman</option>
                            <option value="Giurgiu">Giurgiu</option>
                            <option value="Ialomita">Ialomita</option>
                        </optgroup>
                        <optgroup label="Centru">
                                <option value="Sibiu">Sibiu</option>
                                <option value="Alba">Alba</option>
                                <option value="Mures">Calarasi</option>
                                <option value="Covasna">Covasna</option>
                                <option value="Harghita">Harghita</option>
                                <option value="Brasov">Brasov</option>
                        </optgroup>
                    </select>
                    <select name="inputmeserie" class="selectare-meserie-cautare" style="display: block;">
                      <option selected disabled>Selecteaza meseria!</option>
                          {% for meserie in oferte %}
                           <option value="{{ meserie.denumireMeserie }}">{{ meserie.denumireMeserie }}</option>
                          {% endfor %}
                  </select>
                        <div class="form-group form-group-custom">
                            <input type="submit" value="Cauta!" class="btn btn-secondary btn-selectare-judet-cautare">
                        </div>                   
                </form>
            </div>              
{% endblock %}

locuri de munca模板

{% extends 'base.html' %}
{% load static %}
{% block content %}
            <div class="container container-munca">
                {% if pagina %}
                <h1 class="heading-munca">Locuri de munca de {{meserie}} din judetul {{judet}}</h1>
                <div class="row">
                {% for oferta in pagina %}
                    <div class="col-md-4">
                        <div class="card card-custom">
                            <div class="card-body">
                              <h5 class="card-title">{{oferta.denumireMeserie}}</h5>
                              <h6 class="card-subtitle mb-2 text-muted">{{oferta.agentEconomic}}</h6>
                              <p class="card-text">{{oferta.adresa}}</p>
                              <a href="#" class="card-link">Card link</a>
                            </div>
                        </div> 
                    </div>
                {% endfor %}
              </div>
              {% if pagina.has_other_pages %}
              <div class="row">
                <div class="col-md-12">          
                    <ul class="pagination">
                        {% if pagina.has_previous %}
                        <li class="page-item">
                          <a href="?page={{pagina.previous_page_number}}" class="page-link">&laquo;</a>
                        </li>
                        {% else %}
                        <li class="page-item disabled">
                            <a class="page-link">&laquo;</a>
                          </li>
                        {% endif %}
                          <li class="page-item active">
                            <a class="page-link">1</a>
                          </li>
                        {% if pagina.has_next %}
                        <li class="page-item">
                          <a href="?page={{pagina.next_page_number}}" class="page-link">&raquo;</a>
                        </li>
                        {% else %}
                        <li class="page-item disabled">
                            <a class="page-link">&raquo;</a>
                        </li>
                        {% endif %}
                    </ul>
                </div>
            </div>
            {% endif %}
                {% else %}
                <div class="row">
                    <div class="col-md-12">
                        <div class="card card-custom">
                            <div class="card-body">
                              <h5 class="card-title" style="text-align: center;">Nu am putut gasi rezultate!</h5>
                            </div>
                        </div> 
                    </div>
                </div>
                {% endif %}            
            </div>              
{% endblock %}

cautare =搜索,locuri de munca =工作。 基本上,我想制作一个小型应用程序,使某人可以从特定县的数据库中搜索现有工作。 (朱迪特=县)

那么我该如何使分页工作呢?现在,当我更改页面时,没有任何结果。

0 个答案:

没有答案