added search on Sujet with django-watson

This commit is contained in:
Arthur Gerbaud
2016-08-26 10:17:59 +02:00
parent 6bdde54ed2
commit dd85cb320a
8 changed files with 48 additions and 64 deletions

View File

@@ -0,0 +1 @@
default_app_config = 'sujets.apps.SujetsConfig'

View File

@@ -1,5 +1,9 @@
from django.apps import AppConfig
from watson import search as watson
class SujetsConfig(AppConfig):
name = 'sujets'
def ready(self):
Sujet = self.get_model("Sujet")
watson.register(Sujet, fields=('nom', 'prenom', 'surnom'))

View File

@@ -5,7 +5,28 @@
<div class="panel-heading text-center">
<h3 class="panel-title">Sujets rencontrés</h3>
</div>
<div class="panel-body">
<form action="" method="POST" class="form form-group">{% csrf_token %}
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">
<span class=" glyphicon glyphicon-search"></span>
</span>
<input type="text" name="q" class="form-control" placeholder="Chercher un sujet" aria-describedby="basic-addon1">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Chercher</button>
</span>
</div>
</form>
{% if query_text %}<div class="well well-sm text-center">
<p class="label label-warning">'{{query_text}}'</p>
<p class="label label-info">
{% if not object_list %}Aucun résultat
{% else %} {{ object_list.count }} résultat(s)
{% endif %}
</p>
</div>{% endif %}
</div>
{% if object_list %}
<!-- Table -->
<table class="table table-striped table-hover">
{% for sujet in object_list %}
@@ -15,15 +36,15 @@
</tr>
{% endfor %}
</table>
{% if is_paginated %}
<div class="panel-footer text-center">
{% if is_paginated %}
<div class="panel-footer text-center">
<ul class="pagination">
{% for num in page_obj.paginator.page_range %}
<li {% if page_obj.number == num %} class="active" {%endif%}><a href="?page={{num}}">{{num}}</a></li>
{%endfor%}
</ul>
</div>
{% endif %}
</div>
{% endif %}{% endif %}
</div>
</div>
<div class="col-lg-3 col-md-3">

View File

@@ -1,4 +1,4 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.views import generic
from .models import Sujet
@@ -38,7 +38,18 @@ class SujetListView(generic.ListView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.insert_menu("suivi/menu_sujets.html")
def post(self, request, **kwargs):
from watson import search as watson
search_text = request.POST.get('q')
results = watson.filter(Sujet, search_text)
if results.count() == 1:
return redirect(results[0].get_absolute_url())
self.queryset = results
return self.get(request, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['query_text'] = self.request.POST.get('q', None)
return context
@webpage