I think your code even outputs each student name as many times as there are students in the current class (minus one if the student is enrolled in the class) :)
Your current logic is
for student in all_student: # Alice, Bob, Claude
for enrolled_student in current_class.enrolled_students: # Alice, Bob
if student != enrolled_student:
print student
The output of the above would be
Alice, Alice, Bob, Bob, Claude, Claude, Claude
(first iteration: Alice == Alice, skip. Second: Alice != Bob, print. Third: Alice != Claude, print, etc.)
Also your relationship setup is not quite "idiomatic sqlalchemy". Have a look at http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#many-to-many If you set up your models as in the example (e.q. using the secondary
parameter of the relationship()
function), you'll be able to do something like
% for st in students:
% if st not in class.enrolled_students:
<input type="checkbox" name="student_id" value="${ st.id }" >${ st.id } - ${ st.forename } ${ st.surname }</input><br/>
% endif
% endfor