当我在django中使用'chain'(多模型)时,可以使用分页器来搜索结果

问题描述 投票:2回答:1

我想问一下当我使用中文时,是否有任何方法可以向我的搜索结果中添加分页器,我的意思是这样(多数据库)

我已经尝试了很多方法来执行此操作,但是它不起作用

我在views.py中有这个

def search(request):
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= list(chain(home_database,pcprograms_database,androidapk_database,androidgames_database,antiruvs_database,systems_database,pcgames_database))


        paginator = Paginator(results, 2) # Show 25 rows per page
        page = request.GET.get('search-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="?q={{ request.GET.q }}&submit=Search={{ results.previous_page_number }}">&laquo;</a></li>
     {% else %}
       <li class="disabled"><span>&laquo;</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="?q={{ request.GET.q }}&submit=Search={{ i }}">{{ i }}</a></li>
       {% endif %}
     {% endfor %}
     {% if results.has_next %}
       <li><a href="?q={{ request.GET.q }}&submit=Search={{ results.next_page_number }}">&raquo;</a></li>
     {% else %}
       <li class="disabled"><span>&raquo;</span></li>
     {% endif %}
   </ul>
 {% endif %}


 {% else %}
 <h3> No results for this search </h3>

 {% endif %}

 {% endif %}

所有这些以及更多对我没有帮助。因此,任何想法都会对我有所帮助,谢谢!

python django search django-models django-views
1个回答
0
投票

在模板中,像这样更改您的锚标记

{% if results.has_previous %}
    <li><a href="?search-page={{ results.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">&laquo;</a></li>
{% else %} 
© www.soinside.com 2019 - 2024. All rights reserved.