diff --git a/lms/djangoapps/courseware/courses.py b/lms/djangoapps/courseware/courses.py index 2c46248456f2..bbf9d5394705 100644 --- a/lms/djangoapps/courseware/courses.py +++ b/lms/djangoapps/courseware/courses.py @@ -53,6 +53,7 @@ from lms.djangoapps.courseware.masquerade import check_content_start_date_for_masquerade_user from lms.djangoapps.courseware.model_data import FieldDataCache from lms.djangoapps.courseware.block_render import get_block +from lms.djangoapps.courseware.utils import is_empty_html from lms.djangoapps.grades.api import CourseGradeFactory from lms.djangoapps.survey.utils import SurveyRequiredAccessError, check_survey_required_and_unanswered from openedx.core.djangoapps.content.block_structure.api import get_block_structure_manager @@ -418,7 +419,10 @@ def get_course_about_section(request, course, section_key): if about_block is not None: try: - html = about_block.render(STUDENT_VIEW).content + # Only render XBlock if content exists to avoid generating empty wrapper divs + content = about_block.data + if not is_empty_html(content): + html = about_block.render(STUDENT_VIEW).content except Exception: # pylint: disable=broad-except html = render_to_string('courseware/error-message.html', None) log.exception( diff --git a/lms/djangoapps/courseware/utils.py b/lms/djangoapps/courseware/utils.py index 5409c89f636b..f9ef351dfdc3 100644 --- a/lms/djangoapps/courseware/utils.py +++ b/lms/djangoapps/courseware/utils.py @@ -4,6 +4,7 @@ import datetime import hashlib import logging +from bs4 import BeautifulSoup from django.conf import settings from django.http import HttpResponse, HttpResponseBadRequest @@ -229,3 +230,16 @@ def _use_new_financial_assistance_flow(course_id): ): return True return False + + +def is_empty_html(html_content): + """ + Check if HTML content is effectively empty. + """ + if not html_content: + return True + + soup = BeautifulSoup(html_content, 'html.parser') + text = soup.get_text(strip=True) + + return not text