0

Is there a way to check if the student exsist in the database class table, since student is assigned as a forgeinkey in the table. I want to achive this without open another loop. index.html:

<ul>
{% for student in studentList %}
    {% if student in classList %}
        <li><a href="#">{{ student.name }}</a></li>
    {% endif %}
{% endfor %}

views.py

def index(request):
context = {
    'studentList': Student.objects.all(),
    'classList': ClassRoom.objects.all(),
}
return render(request, 'resurs/index.html', context)
1
  • I added bit more information, student is assigned as a forgeinkey to a classroom in the database. Commented Apr 2, 2018 at 11:15

1 Answer 1

2

Your question is not clear. You don't have "another loop".

However this is the wrong way to do this - it is terribly inefficient. Instead, loop through the students in each class. Assuming Student has a ForeignKey to Class:

{% for class in classList %}
    {% for student in class.student_set.all %}
        ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i see now what you mean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.