views: Fix 'internal server error' when showing 404 page

Closes: #2517.

Tests:

- Without the patch, run without --develop option and visit a non-exiting page
like /plinth/foo/. It results in '500 internal server error' instead of 404
non-found error.

- With the patch, the '404' page is shown. Breadcrumbs show only a link to the
home page with home icon.

- Accessing a page like /plinth/apps/bepasty/add?foo redirects it to
/plinth/apps/bepasty/add/?foo.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2025-09-19 11:50:40 -07:00 committed by James Valleroy
parent 2862862161
commit 9ddb83a741
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -75,19 +75,26 @@ def get_breadcrumbs(request: HttpRequest) -> dict[str, dict[str, str | bool]]:
'url_name': url_name
}
url_name = request.resolver_match.url_name
full_url_name = ':'.join(request.resolver_match.app_names + [url_name])
try:
menu_item = menu.Menu.get_with_url_name(full_url_name)
except LookupError:
# There is no menu entry for this page, find it's app.
_add(request.path, _('Here'), full_url_name)
app_url_name = ':'.join(request.resolver_match.app_names + ['index'])
url_name = request.resolver_match.url_name
full_url_name = ':'.join(request.resolver_match.app_names + [url_name])
except AttributeError:
# 404 page: unknown URL, resolver_match is None. Index is home.
menu_item = menu.Menu.get_with_url_name('index')
else:
try:
menu_item = menu.Menu.get_with_url_name(app_url_name)
menu_item = menu.Menu.get_with_url_name(full_url_name)
except LookupError:
# Don't know which app this page belongs to, assume parent is Home.
menu_item = menu.Menu.get_with_url_name('index')
# There is no menu entry for this page, find it's app.
_add(request.path, _('Here'), full_url_name)
app_url_name = ':'.join(request.resolver_match.app_names +
['index'])
try:
menu_item = menu.Menu.get_with_url_name(app_url_name)
except LookupError:
# Don't know which app this page belongs to, assume parent is
# Home.
menu_item = menu.Menu.get_with_url_name('index')
for _number in range(10):
_add(menu_item.url, menu_item.name, menu_item.url_name)