Skip to content

Commit

Permalink
Let Django serve media files if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Dec 29, 2024
1 parent 1642ace commit f1176dc
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/comics/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.urls import include, path, re_path
from django.views.generic.base import TemplateView
from django.views.static import serve

urlpatterns = [
# Robots not welcome
Expand Down Expand Up @@ -30,9 +32,15 @@
]


# Let Django host media if doing local development on runserver
# Let Django host media if nothing in front of it handles the request.
if not settings.MEDIA_URL.startswith("http"):
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
re_path(
r"^%s(?P<path>.*)$" % re.escape(settings.MEDIA_URL.lstrip("/")),
serve,
kwargs={"document_root": settings.MEDIA_ROOT},
),
]


# Include debug_toolbar during development
Expand Down

0 comments on commit f1176dc

Please sign in to comment.