| 1 |
import re |
| 2 |
from urllib.parse import urlsplit |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.core.exceptions import ImproperlyConfigured |
| 6 |
from django.urls import re_path |
| 7 |
from django.views.static import serve |
| 8 |
|
| 9 |
|
| 10 |
def static(prefix, view=serve, **kwargs): |
| 11 |
""" |
| 12 |
Return a URL pattern for serving files in debug mode. |
| 13 |
|
| 14 |
from django.conf import settings |
| 15 |
from django.conf.urls.static import static |
| 16 |
|
| 17 |
urlpatterns = [ |
| 18 |
# ... the rest of your URLconf goes here ... |
| 19 |
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
| 20 |
""" |
| 21 |
if not prefix: |
| 22 |
raise ImproperlyConfigured("Empty static prefix not permitted") |
| 23 |
elif not settings.DEBUG or urlsplit(prefix).netloc: |
| 24 |
# No-op if not in debug mode or a non-local prefix. |
| 25 |
return [] |
| 26 |
return [ |
| 27 |
re_path( |
| 28 |
r"^%s(?P<path>.*)$" % re.escape(prefix.lstrip("/")), view, kwargs=kwargs |
| 29 |
), |
| 30 |
] |