django 代码中获取静态文件全路径
method 1:
1.先获取基础地址
site = Site.objects.get_current()
path = settings.MEDIA_URL + 'xx/xx.xx'
2.拼接
url_str = '{protocol}://{domain}{path}'.format(
protocol=getattr(settings, 'ABSOLUTEURI_PROTOCOL', 'http'),
domain=site.domain,
path=path
)
method 2:
使用工具 django-absoluteuri 1.2.0
安装:
pip install django-absoluteuri
配置:
INSTALLED_APPS = (
# ...
'django.contrib.sites',
'absoluteuri',
)
ABSOLUTEURI_PROTOCOL = 'https'
使用:
absoluteuri.build_absolute_uri('/some/path/')
![]()


浙公网安备 33010602011771号