Django簡(jiǎn)單博客實(shí)戰(zhàn)(六)---搜索功能

Django-haystack插件實(shí)現(xiàn)

項(xiàng)目地址:https://github.com/ylpxzx/lifeblog

步驟

  1. 安裝依賴包
pip install whoosh,jieba,django-haystack

# 盡量采用其他源的pip進(jìn)行安裝,比如
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple django-haystack
  1. 將haystack加入INSTALLED_APPS
INSTALLED_APPS = [
    'django.contrib.admin',
    ...
    'post',
    'haystack',   #添加該行
]
  1. 在需要搜索的應(yīng)用app下創(chuàng)建search_indexes.py,如在post應(yīng)用下。
from haystack import indexes
from .models import Post

# 類名的命名規(guī)則是固定的,嚴(yán)格按照“應(yīng)用名+Index”
class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True,template_name='search/indexes/post/post_text.txt')

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        return self.get_model().objects.all()
  1. 將site-packages/haystack/backends/whoosh_backend.py復(fù)制到應(yīng)用app下,并更名為whoosh_cn_backend.py

  2. 在settings.py中設(shè)置haystack配置

# 配置搜索設(shè)置
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'post.whoosh_cn_backend.WhooshEngine',  # post應(yīng)用下的whoosh_cn_backend.py文件
        'PATH': os.path.join(BASE_DIR, 'whoosh_index'),  # 指定了索引文件需要存放的位置,我們?cè)O(shè)置為項(xiàng)目根目錄 BASE_DIR 下的 whoosh_index 文件夾(在建立索引時(shí)會(huì)自動(dòng)創(chuàng)建)。
    },
}
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 6  # 指定如何對(duì)搜索結(jié)果分頁(yè),這里設(shè)置為每 6 項(xiàng)結(jié)果為一頁(yè)。
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'  # 指定什么時(shí)候更新索引,這里我們使用 haystack.signals.RealtimeSignalProcessor,作用是每當(dāng)有文章更新時(shí)就更新索引。由于博客文章更新不會(huì)太頻繁,因此實(shí)時(shí)更新沒有問題。
  1. 創(chuàng)建template/search/indexes/app_name/model_text.txt文件,建立指定的字段索引
# 如:創(chuàng)建template/search/indexes/post/post_text.txt
{{ object.title }}
{{ object.content }}
  1. 創(chuàng)建索引,自動(dòng)生成whoosh_index文件夾
# 創(chuàng)建索引
python manage.py rebuild_index  # 輸入y即可

# 更新索引
python manage.py update_index
  1. 在應(yīng)用app的views.py下繼承SearchView
from haystack.views import SearchView
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from django.conf import settings
from django.shortcuts import render,redirect
from django.db.models import Q
class MySearchIndex(SearchView):

    # template = 'search.html'

    def extra_context(self):
        context = super(MySearchIndex,self).extra_context()
        context['category_list'] = Category.objects.all().order_by('post_category')
        context['popular_post'] = Post.objects.all().order_by('-total_views')[:4]
        return context

    def create_response(self):
        if self.request.GET.get('q'):
            # print("進(jìn)入not self.request")
            keyword = self.request.GET.get('q')
            # post_info = Post.objects.filter(title__contains=keyword)
            post_info = Post.objects.filter(Q(title__icontains=keyword)|Q(content__icontains=keyword)).order_by("id")  # 搜索標(biāo)題和文章內(nèi)容
            # post_info = Post.objects.all()
            paginator = Paginator(post_info, settings.HAYSTACK_SEARCH_RESULTS_PER_PAGE)
            try:
                page = paginator.page(int(self.request.GET.get('page', 1)))
            except PageNotAnInteger:
                page = paginator.page(1)
            except EmptyPage:
                page = paginator.page(paginator.num_pages)

            context = {
                'query': self.query,
                'form': self.form,
                'page': page,
                'paginator': paginator,
                'suggestion': None,
            }
            context.update(self.extra_context())
            return render(self.request, self.template, context)
        else:
            qs = super(MySearchIndex, self).create_response()
            # print(self.get_context())
            return qs
  1. 在應(yīng)用urls下配置路由
url(r'^search/$', MySearchIndex(), name='haystack_search'),
  1. 創(chuàng)建template/search/search.html文件
{% extends 'index.html' %}
{% block Banner %}{% endblock %}

{% block area %}
    <div class="col-lg-8">
            <div class="blog_left_sidebar">
                <div class="row">
                    {% for post in page.object_list %}
                    <div class="col-md-6">
                        <article class="blog_style1 small">
                            <div class="blog_img">
                                <img class="img-fluid" style="height: 280px;" src="/media/{{ post.cover }}" alt="">
                            </div>
                            <div class="blog_text">
                                <div class="blog_text_inner">
                                    <div class="cat">
                                        <a class="cat_btn" href="#">{{ post.category }}</a>
                                        <a href="#"><i class="fa fa-calendar" aria-hidden="true"></i> {{ post.create_time|date:"Y-m-d" }}</a>
                                        <a href="#"><i class="fa fa-comments-o" aria-hidden="true"></i> 05</a>
                                    </div>
                                    <a href="{% url 'post:detail' post.id %}" ><h4>{{ post.title }}</h4></a>
                                    <p>{{ post.short_detail|safe }}</p>
                                    <a class="blog_btn" href="{% url 'post:detail' post.id %}">Read More</a>
                                </div>
                            </div>
                        </article>
                    </div>
                    {% empty %}
                    <h3>沒有找到相關(guān)文章</h3>
                    {% endfor %}
                </div>



                <!-- 分頁(yè)欄 -->
                <nav class="blog-pagination justify-content-center d-flex">
                    <ul class="pagination">
                        {% if page.has_previous %}
                        <li class="page-item">
                            <a href="{% url 'post:haystack_search' %}?q={{ query }}&page={{ page.previous_page_number }}" class="page-link" aria-label="Previous">
                                <span aria-hidden="true">
                                    <span class="lnr lnr-chevron-left"></span>
                                </span>
                            </a>

                        </li>
                        {% else %}
                        <li class="page-item">
                            <a href="#" class="page-link" aria-label="Previous">
                                <span aria-hidden="true">
                                    <span class="lnr lnr-chevron-left"></span>
                                </span>
                            </a>
                        </li>
                        {% endif %}

                        {% for num in page.paginator.page_range %}
                            {% if num == page.number %}
                                <li class="page-item active"><a href="{% url 'post:haystack_search' %}?q={{ query }}&page={{ num }}" class="page-link">{{ num }}</a></li>
                            {% else %}
                                <li class="page-item"><a href="{% url 'post:haystack_search' %}?q={{ query }}&page={{ num }}" class="page-link">{{ num }}</a></li>
                            {% endif %}
                        {% endfor %}

                        {% if page.has_next %}
                        <li class="page-item">
                            <a href="{% url 'post:haystack_search' %}?q={{ query }}&page={{ page.next_page_number }}" class="page-link" aria-label="Next">
                                <span aria-hidden="true">
                                    <span class="lnr lnr-chevron-right"></span>
                                </span>
                            </a>
                        </li>
                        {% else %}
                        <li class="page-item">
                            <a href="#" class="page-link" aria-label="Next">
                                <span aria-hidden="true">
                                    <span class="lnr lnr-chevron-right"></span>
                                </span>
                            </a>
                        </li>
                        {% endif %}
                    </ul>
                </nav>

            </div>

                    </div>
{% endblock %}

網(wǎng)上示例需要在字段選取中間加個(gè)object,如:

<p>{{ post.object.short_detail|safe }}</p>

項(xiàng)目文件結(jié)構(gòu)

在這里插入圖片描述

異常解決

  • ImportError: cannot import name 'six' from 'django.utils'
  1. 安裝six
pip install six
  1. 將six.py復(fù)制到django/utils

文件路徑

site-packages/six.py

site-packages/django/utils

  • ImportError: cannot import name python_2_unicode_compatible
  1. 報(bào)錯(cuò)位置導(dǎo)入的包替代為以下導(dǎo)入語(yǔ)句
from django.utils.six import python_2_unicode_compatible
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容