简体   繁体   English

如何在渲染方法的url中添加参数-Django?

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

How to add parameters in a url in render method - Django? 如何在渲染方法的url中添加参数-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 . 您缺少的是,当您从queryset过滤了数据并对其进行了分页时,显然要查看下一页,您需要通过传递相同的filter对象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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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