简体   繁体   中英

How to add parameters in a url in render method - Django?

How to add parameters in a url in render method - Django?

I'm having difficulty adding pagination to a search result.

On the first page the result is shown perfectly, but from the second page onwards, the search parameter no longer exists.

thank you.

def get(self, request):

    clientes = Cliente.objects.filter(
        Q(nome__icontains=request.GET['nome']))

    formPesquisa = FormPesquisaCliente()

    paginator = Paginator(clientes, 40)
    page = request.GET.get('page')
    clientes = paginator.get_page(page)

    response = render(request, 'cliente/index.html', {
        'clientes': clientes,
        'formPesquisa': formPesquisa})

    response['Location'] += '?nome=' +request.GET['nome']
    return response

What are you missing is that when you have filtered data from the queryset and its paginated so obviously to view the next page you need to maintain the state by passing the same filter object nome . So the url should look something like this.

http://localhost:8000/clients/?page=2&nome=something

def get(self, request):
abc = request.GET.get['nome'])     #<--- variable to get value from view
clientes = Cliente.objects.filter(
    Q(nome__icontains=abc))               #<-- pass the abc variable  here

formPesquisa = FormPesquisaCliente()

paginator = Paginator(clientes, 40)
page = request.GET.get('page')
clientes = paginator.get_page(page)

response = render(request, 'cliente/index.html', {
    'clientes': clientes,
    'formPesquisa': formPesquisa,
    'abc':abc})                                      #<-- passing the abc variable to view to maintain the state of your filter.

response['Location'] += '?nome=' +request.GET['nome']
return response

Example Pagination Code:

<div class="pagination">
  <span class="step-links">
      {% if clients.has_previous %}
          <a href="?page=1">&laquo; first</a>
          &nbsp;&nbsp;&nbsp;
          {% if nome %}
          <a href="?page={{ clientes.previous_page_number }}&nome={{ nome }}">previous</a>
            {% else %}
          <a href="?page={{ clientes.previous_page_number }}">previous</a>
            {% endif %}
      {% endif %}
      &nbsp;&nbsp;&nbsp;
      <span class="current">
          Page {{ clientes.number }} of {{ clientes.paginator.num_pages }}.
      </span>
      &nbsp;&nbsp;&nbsp;
      {% if clientes.has_next %}
            {% if nome %}
                <a href="?page={{ clientes.next_page_number }}&nome={{ nome  }}">next</a>
                &nbsp;&nbsp;&nbsp;
                <a href="?page={{ clientes.paginator.num_pages }}&nome={{ nome  }}">last &raquo;</a>
            {% else %}
                <a href="?page={{ clientes.next_page_number }}">next</a>
                &nbsp;&nbsp;&nbsp;
                <a href="?page={{ clientes.paginator.num_pages }}">last &raquo;</a>
            {% endif %}

      {% endif %}
  </span>
</div>

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