Skip to content

Commit

Permalink
Merge branch 'Step26_full-text-search-using-django-haystack' into demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxg committed Jun 15, 2017
2 parents c706280 + b888e01 commit feb4a2f
Show file tree
Hide file tree
Showing 11 changed files with 1,021 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ ENV/

database/
media/
whoosh_index/
.idea/
*.sqlite3
fabfile.py
12 changes: 12 additions & 0 deletions blog/search_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from haystack import indexes
from .models import Post


class PostIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)

def get_model(self):
return Post

def index_queryset(self, using=None):
return self.get_model().objects.all()
1 change: 1 addition & 0 deletions blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
url(r'^archives/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/$', views.ArchivesView.as_view(), name='archives'),
url(r'^category/(?P<pk>[0-9]+)/$', views.CategoryView.as_view(), name='category'),
url(r'^tag/(?P<pk>[0-9]+)/$', views.TagView.as_view(), name='tag'),
# url(r'^search/$', views.search, name='search'),
]
16 changes: 16 additions & 0 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from markdown.extensions.toc import TocExtension

from django.db.models import Q
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView
from django.utils.text import slugify
Expand Down Expand Up @@ -297,3 +298,18 @@ class TagView(ListView):
def get_queryset(self):
tag = get_object_or_404(Tag, pk=self.kwargs.get('pk'))
return super(TagView, self).get_queryset().filter(tags=tag)


"""
def search(request):
q = request.GET.get('q')
error_msg = ''
if not q:
error_msg = "请输入关键词"
return render(request, 'blog/index.html', {'error_msg': error_msg})
post_list = Post.objects.filter(Q(title__icontains=q) | Q(body__icontains=q))
return render(request, 'blog/index.html', {'error_msg': error_msg,
'post_list': post_list})
"""
Loading

0 comments on commit feb4a2f

Please sign in to comment.