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.
 
 
 
 

28 lines
869 B

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