我正在用嗖嗖的搜索引擎实现haystack。当我运行'rebuild_index'时,我的终端出现以下错误。
File "/home/dilts/installingDJANGO/ENV/lib/python3.5/site-packages/django/template/loader.py", line 74, in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: search/indexes/submit_app/incident_text.txt
这个错误在我的浏览器中。
reduce() of empty sequence with no initial value
incidents = SearchQuerySet().autocomplete(content_auto=request.POST.get(title, ''))
return clone.filter(six.moves.reduce(operator.__and__, query_bits))
我的通用文件结构如下所示......
project
|--------submit_app
|--------search_app (uses a submit_app model called Incident)
|--------templates (where I put search/indexes/search_app/incident_text.txt)
从我在网上看到的,我相信我的结构是正确的,但从错误,我不再那么肯定了。我觉得共享模型可能会有些混乱,但我不知道。
settings.朋友
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
WHOOSH_INDEX = os.path.join(BASE_DIR,'whoosh/')
HAYSTACK_CONNECTIONS = {
'default':{
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': WHOOSH_INDEX,
},
}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
views.朋友
from django.shortcuts import render
from .forms import IncidentsSearchForm
from django.contrib.auth.decorators import login_required
from haystack.query import SearchQuerySet
@login_required(login_url='/login/')
def incidents(request):
if request.method == 'POST': # If the form has been submitted...
form = IncidentsSearchForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
title = form.cleaned_data['title']
## this piece isn't working at I had hoped
incidents = SearchQuerySet().autocomplete(content_auto=request.POST.get(title, ''))
else:
form = IncidentsSearchForm() # An unbound form
title = ''
incidents = ''
return render(request, 'search.html',
{
'form': form,
'title': title,
'incidents' : incidents,
}
)
当你包含('haystack.urls')时,默认情况下它会看起来像search / search.html。你有一个search.html文件吗?
/templates/search/search.html
views.朋友
return render(request, 'search/search.html',
{
'form': form,
'title': title,
'incidents' : incidents,
}
我对rebuild_index也有同样的问题。似乎Haystack在模型的app文件夹中查找txt模板。在你的情况下,你应该把索引放入
模板/搜索/索引/ submit_app / incident_text.txt
submit_app不是search_app