0

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

Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116
  • It is not really clear to me what the problem is. You want it to only return *one* row? Which one should it pick if there are multiple? – Willem Van Onsem Jun 12 '18 at 13:47
  • yes the `user_favs` give me many rows, and I want to check everyone one from this rows and filter it .. but without using the for loop,, is this possible? – Ahmed Wagdi Jun 13 '18 at 21:11

1 Answers1

1

Not completely sure, but I'm assuming you're filtering down to only one object in your database but still receiving multiple.

In my experience, this is because pagination and filter sets sometimes have a hard time. Check out this question