1.Django 搜索结果分页的实现

在这里我们用django自带的分页模块来实现

这个paginator对象中带有如下属性:
#per_page:每页显示条目数量
#count:数据总个数
#num_pages:总页数
#page_range:总页数的索引范围,如:(1,10),(1,200)
#page:page对象

view.py


#先导包
from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

#搜索和分页的实现
@csrf_exempt
def content_search(request):
    # 最开始从index.html中的<input type="text" name="content_key" />中获得
    #这里index.html为首页
    content_keywords = request.POST.get('content_key', '')

    if content_keywords == '':
        # 从searchResult.html中的<a href="?pagenum={{good_lists.next_page_number }}&content_keywords={{content_keywords}}">下一页</a>中获得
        #这里searchResult.html为显示搜索结果的那一页
        content_keywords = request.GET.get('content_keywords', '')

    #搜索:注意这里只对内容进行了搜索,对标题的搜索视情况自己补充
    #Bookmark为数据库里全部的需要搜索的内容
    #bookmarks为搜索后的符合条件的内容
    bookmarks = Bookmark.objects.filter(content__icontains=content_keywords)

    #pageum是从前端searchResult.html中的<a href="?pagenum={{good_lists.next_page_number }}&content_keywords={{content_keywords}}">下一页</a>中获得
    #若没有pageum值默认为1
    pagenum = int(request.GET.get('pagenum', 1))

    #分页,将bookmarks(也就是搜索后的内容10个一组分页)
    p = Paginator(bookmarks, 10)
    try:
        good_list = p.page(pagenum)
    except PageNotAnInteger:
        good_list = p.page(1)
    except EmptyPage:
        good_list = p.page(p.num_pages)

    return render(request, 'searchResult.html', {
        'good_lists': good_list,
        'content_keywords': content_keywords,
        #记得把content_keywords返回给前端,保证点击下一页的时候搜索条件不会丢失
    })

searchResult.html

涉及很多样式,复制代码可先删除id和class

<div id="recent-favorites">
		<div class="FYB_RD">
			<table class="c-table opr-toplist1-table">
				<tbody>
				<tbody>
				{% for bookmark in good_lists %}
					<tr>
						<td>
                        <span>
                            <span class="c-index  c-index-hot1 c-gap-icon-right-small">{{ forloop.counter }}</span>
                            <a class="last_collect" title="{{ bookmark.name }}" href="javascript:void(0);"
                               data-url="{{ bookmark.url }}">{{ bookmark.name }}</a>
                        </span>
						</td>
					</tr>
				{% endfor %}
				</tbody>
			</table>
		</div>
    <div>
    
        总页数:<span>{{ good_lists.paginator.num_pages }}</span>
    
        {% if good_lists.has_previous %}
             <a href="?pagenum={{good_lists.previous_page_number}}&content_keywords={{content_keywords}}">上一页</a>
        {% endif %}

        {% for num in good_lists.paginator.page_range %}
             <li id="current-page" {% if num == good_lists.number %}class="active"{% endif %}>
                  <a href="?pagenum={{ num }}&keywords={{ keywords }}">{{ num }}</a>
           </li>
        {% endfor %}

       {% if good_lists.has_next %}
          <a href="?pagenum={{good_lists.next_page_number }}&content_keywords={{content_keywords}}">下一页</a>
        {% endif %}

       当前页:{{ good_lists.number }}
    </div>
</div>

在首页index.html中与此相关联的只有一个

<div id="search">
     <form action="../content_search" method="post">
	      {{ form.as_p }}
	      {% csrf_token %}
	      <input type="text" name="content_key" value="" class="empty" placeholder="请输入关键字" size="50" autofocus="autofocus"/>
    </form>
</div>

2.点击下一页搜索条件丢失可能的原因分析

(1) Get 和 Post 的使用

看看选择下一页时GET内容是否传过去

content_keywords = request.POST.get('content_key', '')
if content_keywords == '':
    content_keywords = request.GET.get('content_keywords', '')

首先input里获取条件,点击下一页后的条件从searchResult.html的content_keywords中获得

(2)

  return render(request, 'searchResult.html', {
        'good_lists': good_list,
        'content_keywords': content_keywords,
        #记得把content_keywords返回给前端,保证点击下一页的时候搜索条件不会丢失
    })

(3)


   <a href="?pagenum={{good_lists.next_page_number }}&content_keywords={{content_keywords}}">下一页</a>
   

{{ }}里放好查询条件

最后由于参数和各种差异,请根据自己的具体程序自行调整

更多推荐

Django 搜索结果分页的实现 以及点击下一页搜索条件丢失可能的原因分析