This commit is contained in:
ddrwode
2026-03-04 17:18:31 +08:00
parent 3303a980f6
commit 8ac650e27c
2 changed files with 16 additions and 7 deletions

View File

@@ -27,6 +27,14 @@ from server.models import ContactRecord
from server.serializers import ContactRecordSerializer
def _normalize_media_url(media_url: str) -> str:
"""Normalize MEDIA_URL so it always ends with '/' and is safe for URL拼接."""
media_url = (media_url or "/media/").strip()
if media_url.startswith(("http://", "https://")):
return f"{media_url.rstrip('/')}/"
return f"/{media_url.strip('/')}/"
@api_view(["GET", "POST"])
def contact_list(request):
if request.method == "GET":
@@ -187,8 +195,10 @@ def contact_export(request):
# 保存文件
wb.save(filepath)
# 生成下载链接
download_url = f"{settings.MEDIA_URL}exports/{filename}"
# 生成下载链接(始终返回可直接访问的完整 URL
media_url = _normalize_media_url(getattr(settings, "MEDIA_URL", "/media/"))
download_path = f"{media_url}exports/{filename}"
download_url = request.build_absolute_uri(download_path)
return api_success({
"download_url": download_url,