I have a problem when trying to show data in a table. The data is being fetched and showing in the logs correctly, but it does not show in the frontend table.
This is the table:
<!-- 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>`
This is the route:
@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'))
<p>Debug: {{ recommended_courses }}</p>
When I add {{ recommended_courses }} for debugging, I see the raw data displayed on the frontend. However, the table itself remains empty. What might be causing this issue?
I have a problem when trying to show data in a table. The data is being fetched and showing in the logs correctly, but it does not show in the frontend table.
This is the table:
<!-- 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>`
This is the route:
@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'))
<p>Debug: {{ recommended_courses }}</p>
When I add {{ recommended_courses }} for debugging, I see the raw data displayed on the frontend. However, the table itself remains empty. What might be causing this issue?
I think the variable recommended_courses
is of type tuple[list[dict]] instead of just list[dict]. Try replacing {% for course in recommended_courses %}
with {% for course in recommended_courses[0] %}
.
Also try removing the or 'None'
from {{ course.prerequisite or 'None' }}
.
Hope this helps.