Hi,
I would like to retrieve my variable ‘foods’ from my result_search function to use it in my saved_substitute function but I can’t find the solution …
def result_search(request):
query = request.GET.get('query')
foods = Food.objects.filter(food__icontains=query)
if foods.count() <= 0:
return render(request, 'off/result_search.html', {
'nofoods': "Aucun aliment trouvé !",
})
else:
return render(request, 'off/result_search.html', {
'foods' : foods,
})
@login_required
def saved_substitute(request):
substitute_food = request.GET.get('saved_substitute')
substitute_food = Food.objects.get(pk=substitute_food)
create_substitute_saved = Substitute.objects.create(user=request.user, food=substitute_food, substitut=substitute_food)
create_substitute_saved.save()
user = Substitute.objects.filter(user=request.user)
paginator = Paginator(user, 5)
page = request.GET.get('page')
paginator_user = paginator.get_page(page)
messages.info(request, 'Votre aliment substitué a été sauvegardé !')
return render(request, 'off/substitute_saved.html', {
'user' : paginator_user,
})
My second problem is that my paginator does not work after page two it returns this error to me(function saved_substitute right up and my temps right down):
[...]
{% endfor %}
</div>
<div class="pagination">
<span class="step-links" style="margin: 0 auto;">
{% if user.has_previous %}
<a href="?page=1">« Début</a>
<a href="?page={{ user.previous_page_number }}">Précédent</a>
{% endif %}
<span class="current">
Page {{ user.number }} sur {{ user.paginator.num_pages }}.
</span>
{% if user.has_next %}
<a href="?page={{ user.next_page_number }}">Suivant</a>
<a href="?page={{ user.paginator.num_pages }}">Fin »</a>
{% endif %}
</span>
</div>
{% endif %}
Thanks for your help !