适用于网络静态文件的Docker Django 404,但适用于管理静态文件的Docker [英] Docker Django 404 for web static files, but fine for admin static files

查看:87
本文介绍了适用于网络静态文件的Docker Django 404,但适用于管理静态文件的Docker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我在此docker django配置中提供静态文件。



我的 Django Docker 上运行的项目遇到了一些问题投放静态文件


用于 admin视图的所有静态文件都可以正常加载,但是用于客户端Web视图的静态文件却抛出404 Not发现错误。


这是我的 docker.yml 配置详细信息:

 网络:
构建:./web
公开:
- 8000
链接:
-postgres:postgres
卷:
-./web:/usr/src/app
端口:
- 8000:8000
env_file:.env
命令:python manage.py runserver 0.0.0.0:8000

postgres:
图片:postgres:最新
卷:
-/ var / lib / postgresql
端口:
- 5432:5432



更新



这是管理静态文件的网址,如下所示:
http://developer.com:8000/static/admin/css/base.css
,这就是客户端的方式静态文件网址看起来像:
http://developer.com: 8000 / static / css / base.css
其中,静态目录中的admin文件夹是通过运行django命令 collectstatic 创建的p>

我以前使用过此设置,并且工作正常。但是,当我将项目根文件夹移动到另一个目录时,似乎会出现此问题。



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

解决方案

这是由于 STATICFILES_DIRS 配置中的问题 settings.py 文件。


此设置定义了启用 FileSystemFinder 查找程序时 staticfiles 应用将遍历的其他位置,例如如果您使用collectstatic或findstatic管理命令或使用静态文件服务视图。


以下是我的settings.py中的配置:

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

现在我将此代码更新为:

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

每个文件都可以正常加载。



参考链接


please help me on this docker django configuration for serving static files.

my Django project running on Docker got some issues with delivering static files.

All static files for admin view is loading fine, but static files for client web view is throwing 404 Not found Error.

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"

update

This is the admin static file url will look like : http://developer.com:8000/static/admin/css/base.css and this is how client static file url looks like: http://developer.com:8000/static/css/base.css Where those admin folder in static directory is creator by running django command 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.

解决方案

This was issue with the STATICFILES_DIRS configuration in the settings.py file.

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.

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"),
]

And every files is loading fine.

Reference Link

这篇关于适用于网络静态文件的Docker Django 404,但适用于管理静态文件的Docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆