我想添加用戶在組表中,我檢查用戶在所有組成員如果然後如果他已經在表中我閃爍的消息,如果不是我添加用戶羣組。但它只對第一個用戶有效。其餘的當您請求我不能在數據庫中添加更多然後一個用戶
classroomMembers = ClassroomMember.query.filter_by(user_id=user_id).all()
你classroomMembers
記錄與user_id=user_id
只有這些記錄得到的邏輯評估假
@main.route('/join-classroom/<int:classroom_id>/<int:user_id>/')
@login_required
def join_classroom(classroom_id, user_id):
classroom = Classroom.query.filter_by(id=classroom_id).first()
if classroom is None:
flash('Invalid classname')
return redirect(url_for('main.classrooms'))
else:
# check if user aleady added or on request
classroomMembers = ClassroomMember.query.filter_by(user_id=user_id).all()
for classroomMember in classroomMembers:
if (classroomMember.user_id == user_id) and (classroomMember.classroom_id == classroom_id):
flash('You are already in Group ')
return redirect(url_for('main.classrooms'))
elif (classroomMember.user_id != user_id) and (classroomMember.classroom_id != classroom_id):
classroomMember = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
db.session.add(classroomMember)
db.session.commit()
flash('Your request has been sent successfully')
return redirect(url_for('main.classrooms'))
elif classroomMember.user_id is None:
classroomMember = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
db.session.add(classroomMember)
db.session.commit()
flash('Your request has been sent successfully')
return redirect(url_for('main.classrooms'))
我解決了這個問題 –