i am trying to make a search view, and the results are shown. so for without pagination the search view works fine but when i added pagination it stopped working. Below is my code
我正在尝试制作搜索视图,并显示结果。所以对于没有分页的搜索视图工作正常但是当我添加分页时它停止工作。以下是我的代码
views.py
def search_query(request):
if request.method == 'GET':
#try:
if 'q' in request.GET:
query_string= request.GET.get('q')
if len(query_string) == 0:
return HttpResponseBadRequest('Invalid Search')
else:
query_list = query_string.split(' ')
posts = _search(query_list)
paginator = Paginator(posts, 1)
page = request.GET.get('page')
if page is None:
page = 1
else:
pass
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
return HttpResponseBadRequest()
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return HttpResponseBadRequest()
context = {
'posts': posts,
}
return render(request, 'search/search.html', {'posts': posts,})
#except:
# return HttpResponseBadRequest('Outside Try')
Function below does the actual searching
以下功能进行实际搜索
def _search(query_list):
result = []
for query in query_list:
query = query.lower()
try:
posts = Post.objects.order_by('-date_created').filter(tag__tag__icontains=query)
for post in posts:
if post in result:
pass
else:
result.append(post)
except:
result = None
return result
below is the snippet of search.html pagination part
下面是search.html分页部分的片段
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
{% if posts.has_previous %}
{% comment %} <a href="?page={{ posts.previous_page_number }}">previous</a> {% endcomment %}
<li class="page-item">
<a class="page-link" href="?q={{ request.GET.q }}&?page={{ posts.previous_page_number }}" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
{% endif %}
<li class="page-item"><a class="page-link" href="?q={{ request.GET.q }}&?page={{ posts.number }}">{{ posts.number }}</a></li>
{% if posts.has_next %}
<li class="page-item">
<a class="page-link" href="?q={{ request.GET.q }}&?page={{ posts.next_page_number }}">Next</a>
</li>
{% endif %}
</ul>
</nav>
now the problem is the value of page here is None when i print(page)
现在问题是当我打印(页面)时页面的值是无
page = request.GET.get('page')
2 个解决方案
#1
0
no need to add
?
beforepage
when page is second argument不需要添加?页面是第二个参数之前的页面
change the code with this one
用这个改变代码
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
{% if posts.has_previous %}
{% comment %} <a href="?page={{ posts.previous_page_number }}">previous</a> {% endcomment %}
<li class="page-item">
<a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.previous_page_number }}" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
{% endif %}
<li class="page-item"><a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.number }}">{{ posts.number }}</a></li>
{% if posts.has_next %}
<li class="page-item">
<a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.next_page_number }}">Next</a>
</li>
{% endif %}
</ul>
</nav>
#2
0
Hi can you do like this
你好,你可以这样做吗?
def search_query(request):
query_params = request.query_params.dict()
from query_params contains what you passed url
来自query_params包含您传递的url
#1
0
no need to add
?
beforepage
when page is second argument不需要添加?页面是第二个参数之前的页面
change the code with this one
用这个改变代码
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
{% if posts.has_previous %}
{% comment %} <a href="?page={{ posts.previous_page_number }}">previous</a> {% endcomment %}
<li class="page-item">
<a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.previous_page_number }}" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
{% endif %}
<li class="page-item"><a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.number }}">{{ posts.number }}</a></li>
{% if posts.has_next %}
<li class="page-item">
<a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.next_page_number }}">Next</a>
</li>
{% endif %}
</ul>
</nav>
#2
0
Hi can you do like this
你好,你可以这样做吗?
def search_query(request):
query_params = request.query_params.dict()
from query_params contains what you passed url
来自query_params包含您传递的url