分页器在我更改页面时重置查询
Paginator resets query as I change page
我正在用 Django 开发应用程序。
我有一个模板,其中显示了我的模型的数据。
该模板有一个搜索栏和一个分页器。
问题是,当我 运行 查询(假设我搜索单词 "home")时,
它显示了第一页的过滤结果,但是当我点击我的分页器进入下一页时,
查询被重置,我得到了未过滤数据(整个数据)的第 2 页。
那么,如何结合搜索栏和分页器对结果的过滤?
这是我的搜索条码:
<form method="GET" action="{% url 'glossario' %}">
<input id="testo_slot_ricerca_semplice" type="text" placeholder="Ricerca terminologia..." name="q" value="{{request.GET.q}}">
<!-- #cerca -->
<button id="search_button" type="submit"><i class="fa fa-search"></i></button>
<!-- cancella query -->
<button id="cancel_search_button" type="submit" onclick="Delete_simple_search()"><i class="fa fa-trash"></i></button>
</form>
这是我的分页器代码:
<nav aria-label="...">
<ul class="pagination">
{% if all_entries.has_previous %}
<li class="page-item">
<a class="page-link" href="?page=1">« first</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.previous_page_number }}">{{ all_entries.previous_page_number }}</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">« first</a>
</li>
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">previous</a>
</li> -->
{% endif %}
<li class="page-item active">
<a class="page-link" href="#">{{ all_entries.number }}<span class="sr-only">(current)</span></a>
</li>
{% if all_entries.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.next_page_number }}">{{ all_entries.next_page_number }}</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.paginator.num_pages }}">Last [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% else %}
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">next</a>
</li> -->
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">Last [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% endif %}
</ul>
</nav>
这是我在 views.py 填充模板中的功能:
def glossario(request):
query = request.GET.get('q')
template = "glossario.html"
all_entries = glossary_entry.objects.all()
if query:
query = request.GET.get('q')
selected_entries = glossary_entry.objects.filter(Q(Acronimo__icontains=query)|Q(Ambito_riferimento__icontains=query)|Q(Autore_definizione__icontains=query))
# Pagination
paginator = Paginator(selected_entries, 10) # Show 25 contacts per page
page = request.GET.get('page')
selected_entries = paginator.get_page(page)
return render(request, template, {'all_entries':selected_entries})
# se non è stata fatta nessuna query
else:
# Pagination
paginator = Paginator(all_entries, 10)
page = request.GET.get('page')
all_entries = paginator.get_page(page)
return render(request, template, {'all_entries':all_entries})
已解决:
来自 views.py 我正在向我的模板传递一个变量 query
,其中包含我在搜索栏中单击“搜索”时提交的 request.GET.get
条款(它们已保存作为我搜索栏输入中的 q
)。
在我的 views.py 中,该变量也在我获得输入之前定义,因此当页面第一次加载时,它的值为 void。
为了修复它,我实现了以下逻辑:
在分页器中,
如果我的搜索栏没有提交任何查询,
我将分页器呈现为
我在问题中回答的那个,
否则,
我连接 url 以同时具有搜索栏
quesryset 变量和分页器页面 queryset 变量。
看这里:
在views.py中:
def glossario(request):
query = request.GET.get('q')
template = "glossario.html"
all_entries = glossary_entry.objects.all()
# if query has been submitted to search bar
if query:
query = request.GET.get('q') database
selected_entries = glossary_entry.objects.filter(Q(Acronimo__icontains=query)|Q(Ambito_riferimento__icontains=query)|Q(Autore_definizione__icontains=query)|Q(Autore_documento_fonte__icontains=query)|Q(Data_inserimento_entry__icontains=query)|Q(Definizione__icontains=query)|Q(Host_documento_fonte__icontains=query)|Q(Id_statico_entry__icontains=query)|Q(Lemma__icontains=query)|Q(Posizione_definizione__icontains=query)|Q(Titolo_documento_fonte__icontains=query)|Q(Url_definizione__icontains=query)|Q(Url_documento_fonte__icontains=query))
# Pagination
paginator = Paginator(selected_entries, 10) # Show 25 contacts per page
page = request.GET.get('page')
selected_entries = paginator.get_page(page)
return render(request, template, {'all_entries':selected_entries, 'query':query})
# if query has NOT been submitted to search bar
else:
# Pagination
paginator = Paginator(all_entries, 10) # Show 25 contacts per page
page = request.GET.get('page')
all_entries = paginator.get_page(page)
return render(request, template, {'all_entries':all_entries, 'query':query})
在模板中:
<nav aria-label="...">
<ul class="pagination">
<!-- Se la q ha ricevuto un input = la search bar ha inserito input -->
{% if query %}
{% if all_entries.has_previous %}
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page=1">« Prima pagina</a>
</li>
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page={{ all_entries.previous_page_number }}">{{ all_entries.previous_page_number }}</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">« Prima pagina</a>
</li>
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">previous</a>
</li> -->
{% endif %}
<li class="page-item active">
<a class="page-link" href="#">{{ all_entries.number }}<span class="sr-only">(current)</span></a>
</li>
{% if all_entries.has_next %}
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page={{ all_entries.next_page_number }}">{{ all_entries.next_page_number }}</a>
</li>
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page={{ all_entries.paginator.num_pages }}">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% else %}
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">next</a>
</li> -->
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% endif %}
<!-- Se la q è vuota = la search bar non ha inserito input -->
{% else %}
{% if all_entries.has_previous %}
<li class="page-item">
<a class="page-link" href="?page=1">« Prima pagina</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.previous_page_number }}">{{ all_entries.previous_page_number }}</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">« Prima pagina</a>
</li>
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">previous</a>
</li> -->
{% endif %}
<li class="page-item active">
<a class="page-link" href="#">{{ all_entries.number }}<span class="sr-only">(current)</span></a>
</li>
{% if all_entries.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.next_page_number }}">{{ all_entries.next_page_number }}</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.paginator.num_pages }}">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% else %}
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">next</a>
</li> -->
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% endif %}
{% endif %}
</ul>
</nav>
搜索条码保持不变。
我正在用 Django 开发应用程序。
我有一个模板,其中显示了我的模型的数据。
该模板有一个搜索栏和一个分页器。
问题是,当我 运行 查询(假设我搜索单词 "home")时,
它显示了第一页的过滤结果,但是当我点击我的分页器进入下一页时,
查询被重置,我得到了未过滤数据(整个数据)的第 2 页。
那么,如何结合搜索栏和分页器对结果的过滤?
这是我的搜索条码:
<form method="GET" action="{% url 'glossario' %}">
<input id="testo_slot_ricerca_semplice" type="text" placeholder="Ricerca terminologia..." name="q" value="{{request.GET.q}}">
<!-- #cerca -->
<button id="search_button" type="submit"><i class="fa fa-search"></i></button>
<!-- cancella query -->
<button id="cancel_search_button" type="submit" onclick="Delete_simple_search()"><i class="fa fa-trash"></i></button>
</form>
这是我的分页器代码:
<nav aria-label="...">
<ul class="pagination">
{% if all_entries.has_previous %}
<li class="page-item">
<a class="page-link" href="?page=1">« first</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.previous_page_number }}">{{ all_entries.previous_page_number }}</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">« first</a>
</li>
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">previous</a>
</li> -->
{% endif %}
<li class="page-item active">
<a class="page-link" href="#">{{ all_entries.number }}<span class="sr-only">(current)</span></a>
</li>
{% if all_entries.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.next_page_number }}">{{ all_entries.next_page_number }}</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.paginator.num_pages }}">Last [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% else %}
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">next</a>
</li> -->
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">Last [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% endif %}
</ul>
</nav>
这是我在 views.py 填充模板中的功能:
def glossario(request):
query = request.GET.get('q')
template = "glossario.html"
all_entries = glossary_entry.objects.all()
if query:
query = request.GET.get('q')
selected_entries = glossary_entry.objects.filter(Q(Acronimo__icontains=query)|Q(Ambito_riferimento__icontains=query)|Q(Autore_definizione__icontains=query))
# Pagination
paginator = Paginator(selected_entries, 10) # Show 25 contacts per page
page = request.GET.get('page')
selected_entries = paginator.get_page(page)
return render(request, template, {'all_entries':selected_entries})
# se non è stata fatta nessuna query
else:
# Pagination
paginator = Paginator(all_entries, 10)
page = request.GET.get('page')
all_entries = paginator.get_page(page)
return render(request, template, {'all_entries':all_entries})
已解决:
来自 views.py 我正在向我的模板传递一个变量 query
,其中包含我在搜索栏中单击“搜索”时提交的 request.GET.get
条款(它们已保存作为我搜索栏输入中的 q
)。
在我的 views.py 中,该变量也在我获得输入之前定义,因此当页面第一次加载时,它的值为 void。
为了修复它,我实现了以下逻辑:
在分页器中,
如果我的搜索栏没有提交任何查询,
我将分页器呈现为 我在问题中回答的那个,否则,
我连接 url 以同时具有搜索栏 quesryset 变量和分页器页面 queryset 变量。
看这里:
在views.py中:
def glossario(request):
query = request.GET.get('q')
template = "glossario.html"
all_entries = glossary_entry.objects.all()
# if query has been submitted to search bar
if query:
query = request.GET.get('q') database
selected_entries = glossary_entry.objects.filter(Q(Acronimo__icontains=query)|Q(Ambito_riferimento__icontains=query)|Q(Autore_definizione__icontains=query)|Q(Autore_documento_fonte__icontains=query)|Q(Data_inserimento_entry__icontains=query)|Q(Definizione__icontains=query)|Q(Host_documento_fonte__icontains=query)|Q(Id_statico_entry__icontains=query)|Q(Lemma__icontains=query)|Q(Posizione_definizione__icontains=query)|Q(Titolo_documento_fonte__icontains=query)|Q(Url_definizione__icontains=query)|Q(Url_documento_fonte__icontains=query))
# Pagination
paginator = Paginator(selected_entries, 10) # Show 25 contacts per page
page = request.GET.get('page')
selected_entries = paginator.get_page(page)
return render(request, template, {'all_entries':selected_entries, 'query':query})
# if query has NOT been submitted to search bar
else:
# Pagination
paginator = Paginator(all_entries, 10) # Show 25 contacts per page
page = request.GET.get('page')
all_entries = paginator.get_page(page)
return render(request, template, {'all_entries':all_entries, 'query':query})
在模板中:
<nav aria-label="...">
<ul class="pagination">
<!-- Se la q ha ricevuto un input = la search bar ha inserito input -->
{% if query %}
{% if all_entries.has_previous %}
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page=1">« Prima pagina</a>
</li>
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page={{ all_entries.previous_page_number }}">{{ all_entries.previous_page_number }}</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">« Prima pagina</a>
</li>
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">previous</a>
</li> -->
{% endif %}
<li class="page-item active">
<a class="page-link" href="#">{{ all_entries.number }}<span class="sr-only">(current)</span></a>
</li>
{% if all_entries.has_next %}
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page={{ all_entries.next_page_number }}">{{ all_entries.next_page_number }}</a>
</li>
<li class="page-item">
<a class="page-link" href="?q={{ query }}&page={{ all_entries.paginator.num_pages }}">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% else %}
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">next</a>
</li> -->
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% endif %}
<!-- Se la q è vuota = la search bar non ha inserito input -->
{% else %}
{% if all_entries.has_previous %}
<li class="page-item">
<a class="page-link" href="?page=1">« Prima pagina</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.previous_page_number }}">{{ all_entries.previous_page_number }}</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">« Prima pagina</a>
</li>
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">previous</a>
</li> -->
{% endif %}
<li class="page-item active">
<a class="page-link" href="#">{{ all_entries.number }}<span class="sr-only">(current)</span></a>
</li>
{% if all_entries.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.next_page_number }}">{{ all_entries.next_page_number }}</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ all_entries.paginator.num_pages }}">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% else %}
<!-- <li class="page-item disabled"></li>
<a class="page-link" href="#" class="page-item disabled">next</a>
</li> -->
<li class="page-item disabled">
<a class="page-link" href="#" class="page-item disabled">Ultima pagina [ {{ all_entries.paginator.num_pages }} ] »</a>
</li>
{% endif %}
{% endif %}
</ul>
</nav>
搜索条码保持不变。