return a url pattern for serving files in debug mode from django conf import settings from django conf urls static import static urlpatterns the rest of your urlconf goes here staticsettings mediaurl documentrootsettings mediaroot noop if not in debug mode or a nonlocal prefix return a url pattern for serving files in debug mode from django conf import settings from django conf urls static import static urlpatterns the rest of your urlconf goes here static settings media_url document_root settings media_root no op if not in debug mode or a non local prefix
import re
from urllib.parse import urlsplit

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.urls import re_path
from django.views.static import serve


def static(prefix, view=serve, **kwargs):
    
    if not prefix:
        raise ImproperlyConfigured("Empty static prefix not permitted")
    elif not settings.DEBUG or urlsplit(prefix).netloc:
        
        return []
    return [
        re_path(
            r"^%s(?P<path>.*)$" % re.escape(prefix.lstrip("/")), view, kwargs=kwargs
        ),
    ]