如何在渲染方法中的 url 中添加参数 - Django?
How to add parameters in a url in render method - Django?
如何在 url 渲染方法中添加参数 - Django?
我很难为搜索结果添加分页。
第一页完美显示结果,但从第二页开始,搜索参数不再存在
谢谢。
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
您缺少的是,当您从查询集中过滤数据并明显分页以查看下一页时,您需要通过传递相同的过滤器对象来维护状态 nome
。所以 url 应该看起来像这样。
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
示例分页代码:
<div class="pagination">
<span class="step-links">
{% if clients.has_previous %}
<a href="?page=1">« first</a>
{% 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 %}
<span class="current">
Page {{ clientes.number }} of {{ clientes.paginator.num_pages }}.
</span>
{% if clientes.has_next %}
{% if nome %}
<a href="?page={{ clientes.next_page_number }}&nome={{ nome }}">next</a>
<a href="?page={{ clientes.paginator.num_pages }}&nome={{ nome }}">last »</a>
{% else %}
<a href="?page={{ clientes.next_page_number }}">next</a>
<a href="?page={{ clientes.paginator.num_pages }}">last »</a>
{% endif %}
{% endif %}
</span>
</div>
如何在 url 渲染方法中添加参数 - Django?
我很难为搜索结果添加分页。
第一页完美显示结果,但从第二页开始,搜索参数不再存在
谢谢。
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
您缺少的是,当您从查询集中过滤数据并明显分页以查看下一页时,您需要通过传递相同的过滤器对象来维护状态 nome
。所以 url 应该看起来像这样。
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
示例分页代码:
<div class="pagination">
<span class="step-links">
{% if clients.has_previous %}
<a href="?page=1">« first</a>
{% 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 %}
<span class="current">
Page {{ clientes.number }} of {{ clientes.paginator.num_pages }}.
</span>
{% if clientes.has_next %}
{% if nome %}
<a href="?page={{ clientes.next_page_number }}&nome={{ nome }}">next</a>
<a href="?page={{ clientes.paginator.num_pages }}&nome={{ nome }}">last »</a>
{% else %}
<a href="?page={{ clientes.next_page_number }}">next</a>
<a href="?page={{ clientes.paginator.num_pages }}">last »</a>
{% endif %}
{% endif %}
</span>
</div>