Fixes #20859: Handle dashboard widget exceptions (#20870)

This commit is contained in:
Jeremy Stretch 2025-11-24 15:40:06 -05:00 committed by GitHub
parent 82171fce7a
commit d9aab6bbe2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -209,7 +209,10 @@ class ObjectCountsWidget(DashboardWidget):
url = get_action_url(model, action='list')
except NoReverseMatch:
url = None
qs = model.objects.restrict(request.user, 'view')
try:
qs = model.objects.restrict(request.user, 'view')
except AttributeError:
qs = model.objects.all()
# Apply any specified filters
if url and (filters := self.config.get('filters')):
params = dict_to_querydict(filters)

View File

@ -1,4 +1,6 @@
from django import template
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _
register = template.Library()
@ -8,4 +10,16 @@ register = template.Library()
def render_widget(context, widget):
request = context['request']
return widget.render(request)
try:
return widget.render(request)
except Exception as e:
message1 = _('An error was encountered when attempting to render this widget:')
message2 = _('Please try reconfiguring the widget, or remove it from your dashboard.')
return mark_safe(f"""
<p>
<span class="text-danger"><i class="mdi mdi-alert"></i></span>
{message1}
</p>
<p class="font-monospace ps-3">{e}</p>
<p>{message2}</p>
""")