我的问题是:
我在django的搜索结果中添加了Paginator
,并在一页中显示了2个项目。共有8个项目。
当我在网站上进行搜索时,将显示2个项目(应正确显示)。还有指向我可以导航到的4个页面的链接,但是当我单击第2或3或4页的链接时,该页面没有显示任何内容。
我需要帮助来了解为什么会发生这种情况。
然后我通过新的修改解决了这个问题:
view.py:
if request.method == 'GET':
query= request.GET.get('q')
submitbutton= request.GET.get('submit')
if query is not None:
home_database= Homepage.objects.filter(Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query))
pcprograms_database= PCprogram.objects.filter(Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query))
androidapk_database= AndroidApks.objects.filter(Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query))
androidgames_database= AndroidGames.objects.filter(Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query))
antiruvs_database= Antivirus.objects.filter(Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query))
systems_database= OpratingSystems.objects.filter(Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query))
pcgames_database= PCgames.objects.filter(Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query))
# results= sorted(chain(home_database,pcprograms_database,androidapk_database,androidgames_database,antiruvs_database,systems_database,pcgames_database),key=attrgetter('name'))
results = list(
sorted(
chain(pcprograms_database,home_database,androidapk_database,androidgames_database,antiruvs_database,systems_database,pcgames_database),
key=attrgetter('name'),
reverse=True # Optional
))
paginator = Paginator(results,6)
page = request.GET.get('page')
results = paginator.get_page(page)
context={'results': results,
'submitbutton': submitbutton}
return render(request, 'html_file/enterface.html', context)
else:
return render(request, 'html_file/enterface.html')
else:
return render(request, 'html_file/enterface.html')
html文件:
{% if submitbutton == 'Search' and request.GET.q != '' %}
{% if results %}
<h2> Results for <b><i style="color:#337ab7">{{ request.GET.q }}</i></b> : </h2>
<br/><br/>
{% for result in results %}
<label id="label_main_app"> <img style="margin-top:.3%;margin-left:.3%" id="img_main_app_first_screen" src="{{result.app_image.url}}" alt="no image found !" height="160" width="165" > {{result.name}} <br><br> <p id="p_size_first_page"> {{result.app_contect}} <br> <br> <a href="{{ result.page_url }}" type="button" class="btn btn-primary"><big> See More & Download </big> </a> </p>
</label>
{% endfor %}
<!–– here for moving to make new pages in search if nedded ––>
{% if results.has_other_pages %}
<ul class="pagination">
{% if results.has_previous %}
<li><a href="?page={{ results.previous_page_number }}{% for key,value in request.GET.items %}{% ifnotequal key 'page' %}&{{ key }}={{ value }}{% endifnotequal %}{% endfor %}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in results.paginator.page_range %}
{% if results.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}{% for key,value in request.GET.items %}{% ifnotequal key 'page' %}&{{ key }}={{ value }}{% endifnotequal %}{% endfor %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if results.has_next %}
<li><a href="?page={{ results.next_page_number }}{% for key,value in request.GET.items %}{% ifnotequal key 'page' %}&{{ key }}={{ value }}{% endifnotequal %}{% endfor %}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
{% else %}
<h3> No results for this search </h3>
{% endif %}
{% endif %}
这样我解决了这个问题,谢谢大家