【发布时间】:2020-07-05 10:47:32
【问题描述】:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
亲爱的,我在哪里可以了解有关此 static() 函数如何处理传递的两个参数的更多信息?
【问题讨论】:
标签: django
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
亲爱的,我在哪里可以了解有关此 static() 函数如何处理传递的两个参数的更多信息?
【问题讨论】:
标签: django
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):
"""
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)
"""
if not prefix:
raise ImproperlyConfigured("Empty static prefix not permitted")
elif not settings.DEBUG or urlsplit(prefix).netloc:
# No-op if not in debug mode or a non-local prefix.
return []
return [
re_path(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
]
我发现理解这个函数的最好方法是阅读它的源代码!
【讨论】: