serve media previews
All checks were successful
Build and Push Docker Image / build (push) Successful in 45s

This commit is contained in:
2026-04-30 16:07:49 +02:00
parent 27187c5e53
commit 812065a34b
2 changed files with 25 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
from django.conf import settings
from django.core import mail
from django.test import SimpleTestCase, TestCase, override_settings
from django.urls import reverse
from django.urls import reverse, resolve
@override_settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend", ADMINISTRATOR_EMAIL="admin@example.com")
@@ -45,3 +46,11 @@ class FaviconTests(SimpleTestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "image/svg+xml")
self.assertIn(b"<svg", response.content)
class MediaUrlTests(SimpleTestCase):
def test_media_route_is_registered(self):
match = resolve(f"{settings.MEDIA_URL}items/example.jpg")
self.assertEqual(match.func.__module__, "django.views.static")
self.assertEqual(match.func.__name__, "serve")

View File

@@ -14,10 +14,13 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
import re
from urllib.parse import urlsplit
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from django.urls import include, path, re_path
from django.views.static import serve
from santehray import views
@@ -30,5 +33,13 @@ urlpatterns = [
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# Serve uploaded media through Django so item previews work in preview/production
# deployments too. A dedicated web server or object storage can replace this later.
if settings.MEDIA_URL and not urlsplit(settings.MEDIA_URL).netloc:
urlpatterns += [
re_path(
rf"^{re.escape(settings.MEDIA_URL.lstrip('/'))}(?P<path>.*)$",
serve,
{"document_root": settings.MEDIA_ROOT},
),
]