2011-02-14 09:42:26 +08:00
|
|
|
import re
|
2018-07-24 00:32:46 +08:00
|
|
|
from urllib.parse import urlsplit
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2011-02-14 09:42:26 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import re_path
|
2014-08-12 22:54:42 +08:00
|
|
|
from django.views.static import serve
|
2011-02-14 09:42:26 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
def static(prefix, view=serve, **kwargs):
|
2011-02-14 09:42:26 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Return a URL pattern for serving files in debug mode.
|
2011-02-14 09:42:26 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.conf.urls.static import static
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2011-02-14 09:42:26 +08:00
|
|
|
# ... the rest of your URLconf goes here ...
|
2014-04-02 08:46:34 +08:00
|
|
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
2011-02-14 09:42:26 +08:00
|
|
|
"""
|
2017-01-27 07:45:07 +08:00
|
|
|
if not prefix:
|
2011-02-14 09:42:26 +08:00
|
|
|
raise ImproperlyConfigured("Empty static prefix not permitted")
|
2018-07-24 00:32:46 +08:00
|
|
|
elif not settings.DEBUG or urlsplit(prefix).netloc:
|
2017-02-22 04:22:33 +08:00
|
|
|
# No-op if not in debug mode or a non-local prefix.
|
2017-01-27 07:45:07 +08:00
|
|
|
return []
|
2014-04-02 08:46:34 +08:00
|
|
|
return [
|
2016-10-21 01:29:04 +08:00
|
|
|
re_path(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|