django haystack whoosh not showing any errors also no results

本秂侑毒 提交于 2020-03-23 23:59:20

问题


I am trying to django-haystack whoosh. Django Haystack & Whoosh search working but in page no giving result. I have looked up this similar question too

Django-haystack-whoosh is giving no results

Django Haystack & Whoosh Search Working, But SearchQuerySet Return 0 Results

pip freeze

python==3.6
django==2.0.7
Whoosh==2.7.4

-e git://github.com/django-haystack/django.haystack.git#egg=django-haystack

Here is my code:

search_indexes.py

import datetime
from haystack import indexes
from search.models import Articles

class ArticlesIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document = True, use_template = true
    pub_date = indexes.DateTimeField(model_attr = 'pub_date')
    content_auto = indexes.EdgeNgramField(model_attr='title')

    def get_model(self):
        return Articles

    def index_queryset(self, using=None)
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())

views.py

from .models import Articles
from haystack.query import SearchQuerySet

def home(request):
      article=SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', '')
return render(request, 'home.hmtl')

home.html

<form method='post'action=''>
{% csrf_token %}
<table>
   {{form.as_table}}
    <tr>
       <td><input type='search'></td>
       <td><input type='submit' value= 'Search'></td>
    </tr>
</table>
{% if query %}
<h3>Results</h3>
    {% for article in articles %}
      <p><a href="{{article.object.get_absolute_url}}">{{article.object.title}}</a></p>
    {% empty %}
      <p>No results found </p>
    {% endfor %}
{% endif %}
</form>

models.py

from django.db import models

class Articles(models.Model):
     title = models.CharField(max_length = 255)
     body = models.TextField()
     pub_date = models.DateTimeField(auto_now_add=True)

     def __str__(self):
          return self.title

projectfile/templates/indexes/search/articles_text.txt

    {{object.title}}
    {{object.body}}

settings.py

    INSTALLED_APPS = [
       'haystack',
       'whoosh',
       ]
    WHOOSH_INDEX=[os.path.join(BASE_DIR, 'whoosh/'),]
    HAYSTACK_CONNECTIONS = {
       'default': {
         'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
         'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
            },
          }

I ran python3 manage.py rebuild_index and it said indexing 3 articless and my search_indexes.py file in the app folder.


回答1:


Mistake is in your View, In views you are not rendering the article, so views should be:

def home(request):
  articles = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', '')
  return render(request, 'home.html', {'articles':articles})

Why are you using autocomplete ?



来源:https://stackoverflow.com/questions/51552100/django-haystack-whoosh-not-showing-any-errors-also-no-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!