Flask前端推荐课程表格无法渲染数据的问题求助
Flask前端推荐课程表格无法渲染数据的问题求助
大家好,我最近在做Flask项目的一个功能时遇到了个头疼的问题:后台日志里能看到推荐课程的数据已经正确获取,前端页面加{{ recommended_courses }}调试时也能看到完整的原始数据,但页面上的课程表格就是空的,完全没渲染出任何课程行。
下面是我的相关代码:
前端模板代码
<!-- Debug: {{ recommended_courses }} --> <section id="recommended-courses" class="dashboard-section"> <h2 class="section-title"><i class="fas fa-book-open"></i> Recommended Courses</h2> {% if recommended_courses %} <p>Debug: {{ recommended_courses }}</p> <div class="table-container"> <table class="data-table"> <thead> <tr> <th>Course Code</th> <th>Course Name</th> <th>Credit Hours</th> <th>Course Type</th> <th>Semester</th> <th>Prerequisite</th> </tr> </thead> <tbody> {% for course in recommended_courses %} <tr> <td>{{ course.course_code }}</td> <td>{{ course.course_name }}</td> <td>{{ course.credit_hours }}</td> <td>{{ course.course_type }}</td> <td>{{ course.semester }}</td> <td>{{ course.prerequisite or 'None' }}</td> </tr> {% endfor %} </tbody> </table> </div> {% else %} <p>No recommended courses available.</p> {% endif %} </section>
Flask路由代码
@app.route('/advising', methods=['GET', 'POST']) @login_required def advising(): logger.debug(f"Advising route accessed by user {current_user.id}") # Authorization Check if current_user.user_type_ID != 1: flash('Access denied. You are not an advisor.', 'error') return redirect(url_for('dashboard')) try: # Fetch advisor details advisor_id = functions.get_advisor_id(current_user.id) logger.debug(f"Fetched advisor ID: {advisor_id}") if not advisor_id: flash('Advisor ID not found. Please try again.', 'error') return redirect(url_for('dashboard')) advisor_info = functions.fetch_advisor_info(advisor_id) logger.debug(f"Fetched advisor info: {advisor_info}") if not advisor_info: flash('Unable to load advisor information. Please contact support.', 'error') return redirect(url_for('dashboard')) # Initialize context variables student_info = None gpa_deficit = 0 probation_status = False consecutive_semesters = 0 transcript = None recommended_courses = None # Handle student search if request.method == 'POST' and 'search_student' in request.form: student_id = request.form.get('student_id') logger.debug(f"Received student search request with student_ID: {student_id}") if student_id: try: # Resolve student_ID to user_ID user_id = functions.get_user_id_from_student_id(student_id) if not user_id: logger.warning(f"No user_ID found for student_ID: {student_id}") flash(f"Student with ID {student_id} not found.", 'error') else: # Fetch student info using user_ID student_info = functions.fetch_student_info_by_id(user_id) logger.debug(f"Fetched student info for user_ID {user_id}: {student_info}") if student_info: # Fetch additional student data gpa_deficit = functions.calculate_gpa_deficit(user_id) logger.debug(f"GPA Deficit: {gpa_deficit}") con_sem = functions.probation_semesters_count(user_id) logger.debug(f"Consecutive semesters on probation: {con_sem}") probation_status = functions.check_probation_status(user_id, con_sem) logger.debug(f"Probation Status: {probation_status}") consecutive_semesters = functions.count_consecutive_probation_semesters(user_id) logger.debug(f"Consecutive Semesters on Probation: {consecutive_semesters}") transcript = functions.fetch_student_transcript(user_id) logger.debug(f"Fetched transcript: {transcript}") sttran = functions.student_transcript(user_id) logger.debug(f"Fetched sttran: {sttran}") # Validate transcript structure if not isinstance(sttran, list): logger.error("Transcript is not a list. Invalid structure.") raise ValueError("Transcript data is not in expected format.") # Ensure every item in the transcript is a dictionary sttran = [ course for course in sttran if isinstance(course, dict) and 'grade' in course and 'course_code' in course ] # Filter transcript data filtered = [ course for course in sttran if course['grade'] not in ['W', 'P', 'F', 'FA', 'I'] ] completed_courses = [course['course_code'] for course in filtered] completed_course_types = { course['course_type']: completed_courses.count(course['course_type']) for course in filtered } recommended_courses = functions.recommend_courses( user_id, student_info['cumulative_gpa'], completed_courses, completed_course_types, probation_status, con_sem ) logger.debug(f"Fetched recommended courses: {recommended_courses}") else: logger.warning(f"No data found for user_ID: {user_id}") flash(f"Student with ID {student_id} not found.", 'error') except Exception as e: logger.error(f"Error during student search for student_ID {student_id}: {str(e)}", exc_info=True) flash("An error occurred while searching for the student.", "error") # Fetch availability slots for advisor advisor_slots = functions.fetch_availability_slots(advisor_id) logger.debug(f"Fetched advisor slots: {advisor_slots}") # Render the page with all required context return render_template( 'advising.html', advisor_info=advisor_info, advisor_slots=advisor_slots, student_info=student_info, gpa_deficit=gpa_deficit, probation_status=probation_status, consecutive_semesters=consecutive_semesters, transcript=transcript, recommended_courses=recommended_courses, ) except Exception as e: logger.error(f"Unexpected error in advising route: {str(e)}", exc_info=True) flash('An error occurred while loading the advising page. Please try again later.', 'error') return redirect(url_for('dashboard'))
我已经确认recommended_courses在后台日志和前端调试中都有数据,但表格就是不渲染。有没有大佬能帮我分析一下可能的原因,或者给我一些排查的方向?
备注:内容来源于stack exchange,提问作者Youssef Ahmed
相关产品推荐
相关产品推荐

