Improve search

	Search now look for keywords in both the title and the content
	of the article. It is now case-insensitive.
parent c4f1cf88
Pipeline #1197 passed with stages
in 1 minute and 57 seconds
...@@ -5,6 +5,7 @@ from django.views.generic.edit import UpdateView ...@@ -5,6 +5,7 @@ from django.views.generic.edit import UpdateView
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.db.models import Count from django.db.models import Count
from django.db.models import Q
from django import forms from django import forms
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
...@@ -34,8 +35,10 @@ class ArticleList(ListView): ...@@ -34,8 +35,10 @@ class ArticleList(ListView):
else: else:
qs = Article.objects.filter() qs = Article.objects.filter()
if self.request.GET.get('q', '') != '': search_keywords = self.request.GET.get('q', '')
qs = qs.filter(title__contains=self.request.GET.get('q', '')) if search_keywords != '':
qs = qs.filter(Q(title__icontains=search_keywords)
| Q(extracts__icontains=search_keywords))
filter_tag = self.kwargs.get("filter_tag", self.filter_tag) filter_tag = self.kwargs.get("filter_tag", self.filter_tag)
if filter_tag is not None: if filter_tag is not None:
...@@ -49,7 +52,6 @@ class ArticleList(ListView): ...@@ -49,7 +52,6 @@ class ArticleList(ListView):
qs = Tag.objects.annotate(num_times=Count('taggit_taggeditem_items')).all() qs = Tag.objects.annotate(num_times=Count('taggit_taggeditem_items')).all()
qs = qs.order_by('-num_times') qs = qs.order_by('-num_times')
context["tags"] = qs context["tags"] = qs
context["search"] = self.request.GET.get("q", '')
return context return context
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment