Skip to content

Commit 2536e5a

Browse files
committed
Fix error handling in template tag
1 parent 3c9c7bd commit 2536e5a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

solo/templatetags/solo_tags.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ def get_solo(model_path: str) -> SingletonModel:
1919
"Received '{model_path}'."
2020
).format(model_path=model_path)
2121
)
22-
model_class: type[SingletonModel] = apps.get_model(app_label, model_name)
23-
if not model_class:
22+
try:
23+
model_class: type[SingletonModel] = apps.get_model(app_label, model_name)
24+
except LookupError:
2425
raise template.TemplateSyntaxError(
2526
_("Could not get the model name '{model}' from the application named '{app}'").format(
2627
model=model_name, app=app_label

solo/tests/tests.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.core.cache import caches
22
from django.core.files.uploadedfile import SimpleUploadedFile
3-
from django.template import Context, Template
3+
from django.template import Context, Template, TemplateSyntaxError
44
from django.test import TestCase
55
from django.test.utils import override_settings
66

@@ -15,6 +15,18 @@ def setUp(self):
1515
"{{ site_config.site_name }}"
1616
"{{ site_config.file.url }}"
1717
)
18+
self.template_invalid_app = Template(
19+
"{% load solo_tags %}"
20+
'{% get_solo "invalid_app.SiteConfiguration" as site_config %}'
21+
"{{ site_config.site_name }}"
22+
"{{ site_config.file.url }}"
23+
)
24+
self.template_invalid_model = Template(
25+
"{% load solo_tags %}"
26+
'{% get_solo "tests.InvalidModel" as site_config %}'
27+
"{{ site_config.site_name }}"
28+
"{{ site_config.file.url }}"
29+
)
1830
self.cache = caches["default"]
1931
self.cache_key = SiteConfiguration.get_cache_key()
2032
self.cache.clear()
@@ -95,6 +107,14 @@ def test_cache_prefix_overriding(self):
95107
prefix = key.partition(":")[0]
96108
self.assertEqual(prefix, "other")
97109

110+
def test_template_tag_invalid_app_name(self):
111+
with self.assertRaises(TemplateSyntaxError):
112+
self.template_invalid_app.render(Context())
113+
114+
def test_template_invalid_model_name(self):
115+
with self.assertRaises(TemplateSyntaxError):
116+
self.template_invalid_model.render(Context())
117+
98118

99119
class SingletonWithExplicitIdTest(TestCase):
100120
def setUp(self):

0 commit comments

Comments
 (0)