I want to check of some field is exists in a query set that has many rows as result, here are the files
views.py
def home(request):
all_dress = Item.objects.filter(dress_active=True).order_by('-created_at')
page = request.GET.get('page', 1)
paginator = Paginator(all_dress, 12)
try:
dresss = paginator.page(page)
except PageNotAnInteger:
dresss = paginator.page(1)
except EmptyPage:
dresss = paginator.page(paginator.num_pages)
context = {
'dresss': dresss,
}
return render(request, 'fostania_web_app/home.html', context)
the context_processors.py where i get my user_Favs
query
def include_user_favs(request, user_favs=None):
if request.user.is_anonymous:
pass
else:
user_favs = Favorite.objects.filter(user=request.user)
context = {
'user_favs': user_favs,
}
return (context)
And here is my HTML code :
{% if user.is_authenticated %}
{% if user_favs %}
{% for item in user_favs %}
{% if item.item == dress %}
<a href="{% url 'favorite_item' dress.id %}">
<img src="{% static 'img/star-yes.png' %}" title="مسح من الفساتين المفضلة"></a>
{% else %}
<a href="{% url 'favorite_item' dress.id %}">
<img src="{% static 'img/star_no.png' %}" title="إضافة إلى الفساتين المفضلة"></a>
{% endif %}
{% endfor %}
{% else %}
<a href="{% url 'favorite_item' dress.id %}">
<img src="{% static 'img/star_no.png' %}" title="إضافة إلى الفساتين المفضلة"></a>
{% endif %}
{% endif %}
I simply get a multiple result, as it uses the for
loop to check on every item and if it found more that one item, it gives me more than 1 answer