Created
December 2, 2015 08:20
-
-
Save jirfag/ad9668cba8516e3b8995 to your computer and use it in GitHub Desktop.
Haystack setup for Django 1.8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/settings.py | |
INSTALLED_APPS = ( | |
#... | |
'haystack', | |
) | |
HAYSTACK_CONNECTIONS = { | |
'default': { | |
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', | |
'URL': 'http://127.0.0.1:9200/', | |
'INDEX_NAME': 'haystack', | |
}, | |
} | |
#app/urls.py | |
url(r'^search/', include('haystack.urls', app_name='search', namespace='search')), | |
#qa/search_indexes.py | |
import datetime | |
from haystack import indexes | |
from .models import Question | |
class QuestionIndex(indexes.SearchIndex, indexes.Indexable): | |
text = indexes.CharField(document=True, use_template=True) | |
pub_date = indexes.DateTimeField(model_attr='pub_date') | |
def get_model(self): | |
return Question | |
def index_queryset(self, using=None): | |
"""Used when the entire index for model is updated.""" | |
return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now()) | |
#qa/templates/search/indexes/qa/question_text.txt | |
{{ object.title }} | |
{{ object.text }} | |
{% for answer in object.answer_set.all %} | |
{{ answer.text }} | |
{% endfor %} | |
#qa/templates/search/search.html | |
{% extends 'base.html' %} | |
{% block content %} | |
<div class="row"> | |
<div class="col-md-12"> | |
<form method="get" action="."> | |
<table> | |
{{ form.as_table }} | |
<tr> | |
<td> </td> | |
<td> | |
<input type="submit" value="Search"> | |
</td> | |
</tr> | |
</table> | |
</form> | |
</div> | |
</div> | |
{% if query %} | |
<div class="row"> | |
<div class="col-md-12"> | |
<h3>Результаты поиска</h3> | |
{% for q in page.object_list %} | |
<div class="panel panel-default"> | |
<div class="panel-body"> | |
<a href="{{ q.object.get_absolute_url }}">{{ q.object.title }} (rating: {{ q.object.likes_rating }})</a> | |
</div> | |
<div class="panel-footer">{{ q.object.text|truncatechars:50 }}</div> | |
</div> | |
{% empty %} | |
<p>Ничего не найдено.</p> | |
{% endfor %} | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-12"> | |
{% 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 %} | |
</div> | |
</div> | |
{% endif %} | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment