Skip to content

Commit 8b2a30f

Browse files
hramezanifelixxm
authored andcommitted
Fixed #32285 -- Raised ImproperlyConfigured when AppConfig.label is not a valid Python identifier.
1 parent 110001d commit 8b2a30f

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

django/apps/config.py

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def __init__(self, app_name, app_module):
3434
# This value must be unique across a Django project.
3535
if not hasattr(self, 'label'):
3636
self.label = app_name.rpartition(".")[2]
37+
if not self.label.isidentifier():
38+
raise ImproperlyConfigured(
39+
"The app label '%s' is not a valid Python identifier." % self.label
40+
)
3741

3842
# Human-readable name for the application e.g. "Admin".
3943
if not hasattr(self, 'verbose_name'):

tests/apps/tests.py

+8
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,14 @@ def test_repr(self):
436436
ac = AppConfig('label', Stub(__path__=['a']))
437437
self.assertEqual(repr(ac), '<AppConfig: label>')
438438

439+
def test_invalid_label(self):
440+
class MyAppConfig(AppConfig):
441+
label = 'invalid.label'
442+
443+
msg = "The app label 'invalid.label' is not a valid Python identifier."
444+
with self.assertRaisesMessage(ImproperlyConfigured, msg):
445+
MyAppConfig('test_app', Stub())
446+
439447
@override_settings(
440448
INSTALLED_APPS=['apps.apps.ModelPKAppsConfig'],
441449
DEFAULT_AUTO_FIELD='django.db.models.SmallAutoField',

0 commit comments

Comments
 (0)