You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

static.py 886B

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