Solution 1 :

Try in this way

   <h2>Here is the list of tasks! Start working!</h2>
    {% if task %}
        <ul>
        {% for obj in task %}
            <li>{{ obj }}</li>
        {% endfor %}
        </ul>
    {% else %}
        <p>You dont have anything on this list yet!</p>
    {% endif %}

Its {% if task %} not {% if obj in task %}

Hope this helps you, If anything please let me know

Problem :

I am creating a to do list. I want to display the tasks if the user has any. If not, then display something else. I kept the design simple.

<h2>Here is the list of tasks! Start working!</h2>
    {% if obj in task %}
        <ul>
        {% for obj in task %}
            <li>{{ obj }}</li>
        {% endfor %}
        </ul>
    {% else %}
        <p>You dont have anything on this list yet!</p>
    {% endif %}

The ‘task’ is the queryset and currently consists of 2 objects. But none of them are being displayed. Everything was working fine before I tried to apply the presence check. Now it just jumps to that else statement.

views.py:

def task(request):
    task = Task.objects.filter(user=request.user)
    queryset = task.order_by('-start_date')

    context = {
        'task': queryset,
    }

    return render(request, 'task-list.html', context)

Comments

Comment posted by Ajay Kumar

I provided the answer below, please check it

By