Show number of search results
This commit is contained in:
parent
aa3b2fdd7b
commit
c369fbac1e
|
@ -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)
|
||||
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
div.results { margin-top: 1em; }
|
||||
div.photo { float: right; }
|
||||
div.photo img { max-width: 100%; }
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue