为什么在 docker 镜像中运行我的 Django 应用程序后,下载属性不再起作用?

tho*_*mas 2 django gunicorn

运行 django 应用程序的 docker 映像后,我注意到下载文件不再可用。我得到的唯一的东西是我当前所在的网站页面的副本,而不是请求的文件。

\n

在本地,它运行良好。

\n

这是我的代码的组织方式:

\n

在main/views.py中:

\n
path_to_report = f"media/Reports/{request.user.username}/{request.user.username}{now.hour}{now.minute}{now.second}.txt" \n\n\nreturn render(request, "main/result.html", {"dic_files": dic_files, "nbr":len(files), "dic_small":dic_small, "dic_projects":dic_projects, "path_to_report":f"/app/{path_to_report}"})\n
Run Code Online (Sandbox Code Playgroud)\n

main/result.html中

\n
<a href=/{{path_to_report}} download>\n\n        <button class="btn btn-success" name="rapport" value="rapport"> T\xc3\xa9l\xc3\xa9charger votre rapport</button>\n\n</a>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的dockerfile

\n
# Use the official lightweight Python image.\n\n# https://hub.docker.com/_/python\n\nFROM python:3.8\n\n\n\n# Allow statements and log messages to immediately appear in the Knative logs\n\nENV PYTHONUNBUFFERED True\n\n\n\nEXPOSE 8000\n\n\n\n## api-transport-https installation\n\nRUN apt-get install apt-transport-https ca-certificates gnupg\n\n\n\n# Copy local code to the container image.\n\nENV APP_HOME /app\n\nWORKDIR $APP_HOME\n\nCOPY . ./\n\n\n\n# Install production dependencies.\n\nRUN pip3 install --upgrade pip setuptools wheel\n\nRUN pip3 install -r requirements.txt\n\n\n\nRUN python manage.py makemigrations\n\nRUN python manage.py migrate\n\nRUN python manage.py collectstatic --no-input\n\n\n\nENTRYPOINT ["gunicorn", "myteam.wsgi:application", "--bind=0.0.0.0:8000", "--workers=4", "--timeout=300", "--log-level=debug"]\n
Run Code Online (Sandbox Code Playgroud)\n

小智 5

在你的main/views.py中,尝试替换:

"path_to_report":f"/app/{path_to_report}"
Run Code Online (Sandbox Code Playgroud)

和 :

"path_to_report":path_to_report
Run Code Online (Sandbox Code Playgroud)