Show number of search results

This commit is contained in:
Claude Paroz 2018-05-27 11:22:55 +02:00
parent aa3b2fdd7b
commit c369fbac1e
4 changed files with 15 additions and 2 deletions

View file

@ -4,7 +4,10 @@ from .models import Recette
class SearchForm(forms.Form):
text = forms.CharField(widget=forms.TextInput(attrs={'autofocus': True}))
text = forms.CharField(
widget=forms.TextInput(attrs={'autofocus': True}),
required=False
)
saison = forms.ChoiceField(choices=(('all', "Toutes"),) + Recette.SAISON_CHOICES)
veget = forms.BooleanField(label="Végétarien", required=False)

View file

@ -1,2 +1,3 @@
div.results { margin-top: 1em; }
div.photo { float: right; }
div.photo img { max-width: 100%; }

View file

@ -7,10 +7,16 @@ from .models import Recette
def home(request):
form = SearchForm(request.POST or None)
recettes = []
num_results = None
if request.method == 'POST':
recettes = form.search()
num_results = recettes.count()
return render(request, 'index.html', context={'form': form, 'recettes': recettes})
return render(request, 'index.html', context={
'form': form,
'recettes': recettes,
'num_results': num_results,
})
def recette(request, pk):

View file

@ -8,6 +8,9 @@
<button>Rechercher</button>
</form>
{% if num_results is not None %}
<div class="results">{{ num_results }} résultat(s)</div>
{% endif %}
<ul>
{% for recette in recettes %}
<li><a href="{{ recette.get_absolute_url }}">{{ recette.nom }}</a></li>