From d278c314e11927630590ea5a5d470e7eb91971f2 Mon Sep 17 00:00:00 2001 From: Michaelyin Date: Thu, 6 Jun 2024 16:31:27 +0800 Subject: [PATCH] return 404 --- src/django_lookbook/views.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/django_lookbook/views.py b/src/django_lookbook/views.py index c1fbaef..c7d0253 100644 --- a/src/django_lookbook/views.py +++ b/src/django_lookbook/views.py @@ -169,7 +169,18 @@ def inspect_view(request, slug): preview_name, example_name = slug.split("/") - preview_cls = get_previews()[preview_name.replace("_component", "")] + preview_cls = get_previews().get(preview_name.replace("_component", ""), None) + if not preview_cls: + return render( + request, + "django_lookbook/404.html", + context={ + "sidebar_previews": sidebar_previews, + "previews": get_previews(), + }, + status=404, + ) + preview_instance = preview_cls() query_dict = request_get_to_dict(request) @@ -240,7 +251,10 @@ def preview_view(request, slug): preview_name, example_name = slug.split("/") - preview_cls = get_previews()[preview_name.replace("_component", "")] + preview_cls = get_previews().get(preview_name.replace("_component", ""), None) + if not preview_cls: + raise Http404 + preview_instance = preview_cls() query_dict = request_get_to_dict(request)