Repository: codingjoe/django-pictures (language: python)

Issue:
NoReverseMatch : 'pictures' is not a registered namespace
In the last 1.0rc2 update I came across the error "NoReverseMatch : 'pictures' is not a registered namespace", after comparing the last version with the previous one I noticed that a default configuration for the USE_PLACEHOLDERS was missing, so I was able to fix this by adding to the PICTURES in the file settings.py

PICTURES = {
    "BREAKPOINTS": {
        "xs": 576,
        "s": 768,
        "m": 992,
        "l": 1200,
        "xl": 1400,
    },
    "GRID_COLUMNS": 12,
    "CONTAINER_WIDTH": 1200,
    "FILE_TYPES": ["WEBP"],
    "PIXEL_DENSITIES": [1, 2],
    **"USE_PLACEHOLDERS": False,** <- fixed the problem adding this line
}


Discussion / hints:
Hi @eduardo-victor,

Thank you for reaching out. I believe this is because you didn't set up the placeholder's URLs.

I think we need to improve both the documentation and error handling. Likewise, I believe a more verbose error message would be prudent.

I am curious, why didn't you set up the URL bit, and why aren't you using the placeholders in development?

Cheers!
Johannes
I'm using the library for student purposes, maybe I didn't understand how to set the URL bit.
I came across this "error" while trying to list images registered in an API with the DRF, in this way I managed to correct it, but I will study a little more about the library. However thanks for your attention :)
Cheers.
Hi @eduardo-victor, there are no wrong questions. I'll reopen the issue and try to improve the docs. Software adoption is crucial to the success of this project ;)

Fix patch:
diff --git a/pictures/apps.py b/pictures/apps.py
new file mode 100644
index 0000000..301de2d
--- /dev/null
+++ b/pictures/apps.py
@@ -0,0 +1,8 @@
+from django.apps import AppConfig
+
+
+class PicturesAppConfig(AppConfig):
+    name = "pictures"
+
+    def ready(self):
+        import pictures.checks  # noqa
diff --git a/pictures/checks.py b/pictures/checks.py
new file mode 100644
index 0000000..c2da198
--- /dev/null
+++ b/pictures/checks.py
@@ -0,0 +1,34 @@
+from django.core.checks import Error, Tags, register
+from django.urls import NoReverseMatch, reverse
+
+from . import conf
+
+__all__ = ["placeholder_url_check"]
+
+
+@register(Tags.urls)
+def placeholder_url_check(app_configs, **kwargs):
+    errors = []
+    if conf.get_settings().USE_PLACEHOLDERS:
+        try:
+            reverse(
+                "pictures:placeholder",
+                kwargs={
+                    "alt": "test",
+                    "width": 100,
+                    "ratio": "1x1",
+                    "file_type": "jpg",
+                },
+            )
+        except NoReverseMatch:
+            errors.append(
+                Error(
+                    "Placeholder URLs are not configured correctly.",
+                    hint=(
+                        'PICTURES["USE_PLACEHOLDERS"] is True,'
+                        ' but include("pictures.urls") is missing for your URL config.'
+                    ),
+                    id="pictures.E001",
+                )
+            )
+    return errors


Test patch:
diff --git a/tests/test_checks.py b/tests/test_checks.py
new file mode 100644
index 0000000..2882e1a
--- /dev/null
+++ b/tests/test_checks.py
@@ -0,0 +1,20 @@
+from unittest.mock import Mock
+
+from django.urls import NoReverseMatch
+
+from pictures import checks
+
+
+def test_placeholder_url_check(settings, monkeypatch):
+    """Test that the placeholder URL check works."""
+
+    settings.PICTURES["USE_PLACEHOLDERS"] = True
+    assert not checks.placeholder_url_check({})
+
+    reverse = Mock(side_effect=NoReverseMatch)
+    monkeypatch.setattr(checks, "reverse", reverse)
+
+    assert checks.placeholder_url_check({})
+
+    settings.PICTURES["USE_PLACEHOLDERS"] = False
+    assert not checks.placeholder_url_check({})