Docker Django 404用于web静态文件,但对于pipe理静态文件很好

请帮我在这个dockerDjangoconfiguration服务静态文件。

Docker运行的Django项目在传递static files遇到了一些问题。

所有用于pipe理视图的静态文件加载正常,但客户端Web视图的静态文件正在抛出404未find错误。

这是我的docker.ymlconfiguration细节:

 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" 

更新

这是pipe理静态文件的url将如下所示: http : //developer.com : 8000/ static/admin/css/ base.css这是客户端静态文件url的样子: http : //developer.com : 8000 /static/css/base.css静态目录中的那些admin文件夹是创build者,通过运行django命令collectstatic

我以前使用过这个设置,工作正常。 但是当我把项目根文件夹移动到另一个目录似乎有这个问题。

我完全被困在这里,很多非常感谢您的帮助和反馈。

这是settings.py文件中STATICFILES_DIRSconfiguration的问题。

如果启用FileSystemFinder查找程序,例如,如果使用collectstatic或findstaticpipe理命令或使用静态文件服务视图,则此设置将定义staticfiles应用程序将经过的附加位置。

以下是我的settings.py中的configuration:

 STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, "static") 

现在我更新了这个代码:

 STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ] 

并且每个文件加载正常。

参考链接

当你将你的项目移动到另一个目录时,你的静态目录的path也有可能不同了。 Django in most scenarios use apache, nginx or some other web servers to serve static files 。 需要注意的一点是你的静态目录应该被公开访问。 之前我曾经遇到过这样的问题。 我所做的是我moved static dir to document root mentioned in apache config file

所以把你的静态文件移到apache的doc根目录下,并更新settings.py中的静态目录来引用你的apache doc根目录下的静态目录。 我希望这有帮助。