我是django-haystack的新手。根据文档和教程,我能够基于文档内容(DataBase SQLITE)创建搜索应用程序。下一步,我已更新我的HTML模板以请求更多信息(例如:模型,所需的数据库链接等)
我不知道如何处理这个问题。在我正在使用的文件下面。我的知识仍然很基础,所以任何帮助都会非常感激。
感谢。
search.html
{% extends 'base.html' %}
{% block content %}
<h2> Doc Search Interface</h2>
<form method="get" action=".">
<table>
<P>Select Car Model:</P>
<P><LABEL ACCESSKEY=5><INPUT TYPE=checkbox NAME="CarModel" VALUE="A5"> A5</LABEL><BR>
<LABEL ACCESSKEY=8><INPUT TYPE=checkbox NAME="CarModel" VALUE="A8"> A8</LABEL><BR>
<LABEL ACCESSKEY=3><INPUT TYPE=checkbox NAME="CarModel" VALUE="A3"> A3</LABEL></P>
<tr>
<P>Type and click enter for Search!</P>
<input type="search" id="id_q" name="q" placeholder="Search" >
</tr>
</table>
{% if query %}
<h3>Let´s see if we have got here the document you were looking for...</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
{% endblock %}
models.py
from django.db import models
from django.contrib.auth.models import User
from django.template import RequestContext
from django.http import HttpResponse
class Document(models.Model):
user_id = models.CharField(max_length=6, default='admin')
pub_date = models.DateTimeField()
title = models.CharField(max_length=200)
Link= models.URLField()
content = models.TextField()
CarModel= models.TextField()
def get_absolute_url(self):
return self.Link
def __unicode__(self):
return self.title
search_indexes.py
from haystack import indexes
from test3.models import Document
class DocumentIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
content_auto=indexes.EdgeNgramField(model_attr='content')
CarModel=indexes.EdgeNgramField(model_attr='CarModel')
def get_model(self):
return Document
def index_queryset(self, using=None):
"""Used when the entire index for model is updated. Typically to avoid some results showing when admin do not want to"""
return self.get_model().objects
关注此帖[{3}}后,我更新了以下文件
urls.py
from django.conf.urls import url
from test3.views import MySearchView
# urls.py
urlpatterns = [url(r'^/search/?$', MySearchView.as_view(), name='My_search_view'),]
views.py
from haystack.forms import HighlightedSearchForm
from haystack.query import SearchQuerySet
from haystack.generic_views import SearchView
from haystack.views import search_view_factory
class MySearchView(SearchView):
"""My custom search view."""
def search_posts(request):
post_type = str(request.GET.get('CarModel')).lower()
print (str(request.GET.get('CarModel')).lower())
sqs = SearchQuerySet().filter(CarModel__contains=post_type)
clean_query = sqs.query.clean(post_type)
result = sqs.filter(content=clean_query)
view = search_view_factory(
view_class=SearchView,
template='search/search.html',
searchqueryset=result,
form_class=HighlightedSearchForm
)
return view(request)
但是,我仍然无法正常工作......搜索结果仍未按“模型”字段过滤。我想我必须添加额外的代码,但我不知道在哪里...
答案 0 :(得分:0)
我会尝试回应这个。这就是我的工作方式:
在 yourapp / urls.py
中url(r'^search/$', views.search_notes, name='search_notes'),
在 yourapp / views.py 中导入NotesSearchForm(下面再说)并在搜索时调用它:
from .forms import NotesSearchForm
def search_notes(request):
form = NotesSearchForm(request.GET)
notes = form.search()
return render_to_response('notes/index.html', {'notes': notes, 'form': form})
context = { 'latest_notes_list': latest_notes_list}
return render(request, 'notes/index.html', context)
然后在你的 yourapp / forms.py 中发生魔法:
from haystack.forms import SearchForm
class NotesSearchForm(SearchForm):
# First we add the form fields, in your case BooleanFields.
sports = forms.BooleanField()
fiction = forms.BooleanField()
non_fiction = forms.BooleanField()
def search(self):
# Here we store the SearchQuerySet received from other processing.
sqs = super(NotesSearchForm, self).search()
if not self.is_valid():
return self.no_query_found()
# Then filter your results when the search come back
if self.cleaned_data['sports']:
sqs = sqs.filter(genre='sports')
if self.cleaned_data['fiction']:
sqs = sqs.filter(genre='fiction')
if self.cleaned_data['non_fiction']:
sqs = sqs.filter(genre='non_fiction')
return sqs
def no_query_found(self):
# This we add to make all results show if nothing is selected or searched for (optional)
return self.searchqueryset.all()
请注意,在这里我将'genre'作为模型中字段的名称。此字段也需要出现在 search_indexes.py 中,如下所示:
genre = indexes.CharField(model_attr='genre')
最后,让我告诉你 yourapp / templates / notes / index.html :
<!-- Form here -->
{{ form }}
<!-- Search results here -->
{% for note in notes %}
{{ note.object.title }}
{{ notes.object.body|linebreaks }}">
{% endfor %}
希望它适合你!如果你遇到困难,请告诉我。