一.安装
pip install whoosh django-haystack
在django_test下创建whoosh文件夹
二.修改article/search_indexes.py的内容
import datetime
from haystack import indexes
from article.models import Article
class ArticleIndex(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 Article
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
三.修改templates/search/indexes/article/article_text.txt
{{ object.title }}
{{ object.body }}
四.修改urls.py的内容
from django.conf.urls import patterns, include, url
#from django.contrib.auth.views import login, logout
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
from django_test.forms import ContactForm1, ContactForm2, ContactForm3
from django_test.views import ContactWizard
import settings
urlpatterns = patterns('',
(r'^articles/', include('article.urls')),
(r'^accounts/', include('userprofile.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^facebook/', include('django_facebook.urls')),
# user auth urls
url(r'^accounts/login/$', 'django_test.views.login'),
url(r'^accounts/auth/$', 'django_test.views.auth_view'),
url(r'^accounts/logout/$', 'django_test.views.logout'),
url(r'^accounts/loggedin/$', 'django_test.views.loggedin'),
url(r'^accounts/invalid/$', 'django_test.views.invalid_login'),
url(r'^accounts/register/$', 'django_test.views.register_user'),
url(r'^accounts/register_success/$', 'django_test.views.register_success'),
url(r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2, ContactForm3])),
url(r'^search/', include('haystack.urls')),
url(r'^celery_test/', 'django_test.views.start_celery_task'),
url(r'^celery_progress/', 'django_test.views.monitor_celery_task'),
)
if not settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
五.修改article/views.py
from django.shortcuts import render_to_response, render
from article.models import Article, Comment
from django.http import HttpResponse
from forms import ArticleForm, CommentForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.utils import timezone
from django.conf import settings
from haystack.query import SearchQuerySet
from django.template import RequestContext
from django.contrib import messages
import logging
logr = logging.getLogger(__name__)
def search_titles(request):
articles = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
return render_to_response('ajax_search.html', {'articles' : articles})
六.修改ajax_search.html
{% if articles.count > 0 %}
{% for article in articles %}
<li><a href="{{ article.object.get_absolute_url }}">{{ article.object.title }}</a></li>
{% endfor %}
{% else %}
<li>None to show!</li>
{% endif %}
七.运行
./manage.py rebuild_index