我正在使用Hay堆栈和Whoosh来构建一个站点的搜索引擎部分。在我的情况下,Whoosh工作得很好,但我需要从我的观点显示额外的信息,这取决于搜索发现了什么。
在我的Django视图中,我使用类似的东西,其中虚拟是要显示的信息:
dummy = "dummy"
return render_to_response('images/ib_large_image.html', {'dummy': dummy},
context_instance=RequestContext(request))因此,基本上,我希望个性化的视图的搜索,以显示我的变量到搜索模板。
以下是一些配置:
设置
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
'DEFAULT_OPERATOR': 'AND',
'SITECONF': 'search_sites',
'SEARCH_RESULTS_PER_PAGE': 20
},
}search_sites.py
import haystack
haystack.autodiscover()搜索>索引>图像板> image_text.txt
{{ object.name }}
{{ object.description }}图像板> search_indexes.py
import datetime
from haystack import indexes
from imageboard.models import Image
class ImageIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return Image
def index_queryset(self):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(uploaded_date__lte=datetime.datetime.now())图像板> urls.py
urlpatterns = patterns('imageboard.views',
(r'^search/', include('haystack.urls')),
)我将我的视图配置成这样,但它不起作用:
图像板> views.py
from haystack.views import SearchView
def search(request):
return SearchView(template='search.html')(request)知道吗??
发布于 2012-02-20 02:06:44
我建议你看看干草堆"StoredFields“。这些存储搜索结果视图需要在搜索索引中访问的任何信息。额外的好处是搜索结果视图不需要访问DB就可以呈现它们的内容。此外,您还可以将每个搜索结果的输出预呈现到存储的字段中。
class ImageIndex(indexes.SearchIndex, indexes.Indexable):
rendered = CharField(use_template=True, indexed=False)然后,在名为search/indexes/myapp/image_rendered.txt的模板中:
<h2>{{ object.title }}</h2>
<p>{{ object.content }}</p>最后,在search/search.html中:
...
{% for result in page.object_list %}
<div class="search_result">
{{ result.rendered|safe }}
</div>
{% endfor %}https://stackoverflow.com/questions/9352754
复制相似问题