Add a search form

    It can filter articles depending on their titles
parent 206bfc61
Pipeline #1196 passed with stages
in 1 minute and 54 seconds
...@@ -65,10 +65,12 @@ ...@@ -65,10 +65,12 @@
{% endif %} {% endif %}
<li class="nav-item ml-auto"> <li class="nav-item ml-auto">
<a class="nav-link disabled" href="#"> <form method="get" action="{% url 'rp:public-article-list' %}">
<i class="fa fa-search"></i> <div class="input-group">
Rechercher <input type="search" placeholder="Rechercher" name="q" class="fa fa-search" />
</a> <button type="submit" class="btn btn-default input-group-addon"><i class="fa fa-search"></i></button>
</div>
</form>
</li> </li>
<li class="nav-item"> <li class="nav-item">
......
...@@ -34,6 +34,9 @@ class ArticleList(ListView): ...@@ -34,6 +34,9 @@ class ArticleList(ListView):
else: else:
qs = Article.objects.filter() qs = Article.objects.filter()
if self.request.GET.get('q', '') != '':
qs = qs.filter(title__contains=self.request.GET.get('q', ''))
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:
qs = qs.filter(tags__name__in=[filter_tag]) qs = qs.filter(tags__name__in=[filter_tag])
...@@ -46,6 +49,7 @@ class ArticleList(ListView): ...@@ -46,6 +49,7 @@ 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