Docker Django 404 for web static files, but fine for admin static files(Docker Django 404 用于 web 静态文件,但适用于管理静态文件)
问题描述
请帮我解决这个用于提供静态文件的 docker django 配置.
please help me on this docker django configuration for serving static files.
我在 Docker 上运行的 Django 项目在交付 静态文件 时遇到了一些问题.
my Django project running on Docker got some issues with delivering static files.
管理视图的所有静态文件加载正常,但客户端 Web 视图的静态文件抛出 404 Not found 错误.
All static files for admin view is loading fine, but static files for client web view is throwing 404 Not found Error.
这是我的 docker.yml 配置详情:
This is my docker.yml configuration details:
web:
build: ./web
expose:
- "8000"
links:
- postgres:postgres
volumes:
- ./web:/usr/src/app
ports:
- "8000:8000"
env_file: .env
command: python manage.py runserver 0.0.0.0:8000
postgres:
image: postgres:latest
volumes:
- /var/lib/postgresql
ports:
- "5432:5432"
更新
这是管理静态文件的 url,如下所示:http://developer.com:8000/static/admin/css/base.css这就是客户端静态文件 url 的样子:http://developer.com:8000/static/css/base.css通过运行 django 命令 collectstatic
我以前使用过此设置,并且工作正常.但是当我将项目根文件夹移动到另一个目录时似乎有这个问题.
I have used this setting previously, and was working fine. But when I moved the project root folder to another directory seems have this issue.
我完全被困在这里,非常感谢您的所有帮助和反馈.
I am totally stuck here, many many thanks for all your help and feedback.
推荐答案
这是 settings.py<中 文件.STATICFILES_DIRS 配置的问题/code>
此设置定义了 staticfiles 应用程序在启用 FileSystemFinder 查找器时将遍历的其他位置,例如如果您使用 collectstatic 或 findstatic 管理命令或使用静态文件服务视图.
This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.
以下是我的 settings.py 中的配置:
Following was the configuration in my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
现在我将这段代码更新为:
Now I updated this code to:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
每个文件都加载正常.
参考链接
这篇关于Docker Django 404 用于 web 静态文件,但适用于管理静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Docker Django 404 用于 web 静态文件,但适用于管理静态文件
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
