Django 404 上的静态文件,同时设置了 static_root 和 static_url
Posted
技术标签:
【中文标题】Django 404 上的静态文件,同时设置了 static_root 和 static_url【英文标题】:Static files on Django 404 with static_root and static_url both set 【发布时间】:2021-07-31 16:34:40 【问题描述】:我知道这个问题已经得到了很多回答,但我似乎无法解决这个问题。我的静态文件在那里,在我告诉 django 的位置。 Debug 设置为 True,static_url 设置为 '/static/' 但静态文件是 404ing。
开发(设置).py
DEBUG = True
logger.info(f"Debug is DEBUG")
STATIC_URL = "/static/"
STATIC_ROOT = "/home/phillyharper/dask/static"
logger.info(STATIC_ROOT)
使用这些设置运行 python manage.py runserver
会产生以下结果:
2021-05-09 07:17:03.197 | INFO | dask.settings.development:<module>:4 - Debug is True
2021-05-09 07:17:03.197 | INFO | dask.settings.development:<module>:47 - Debug is True
2021-05-09 07:17:03.197 | INFO | dask.settings.development:<module>:50 - /home/phillyharper/dask/static
2021-05-09 07:17:03.198 | INFO | dask.settings:<module>:7 - DEV SETTINGS SETTINGS
2021-05-09 01:47:03.417 | INFO | dask.settings.development:<module>:4 - Debug is True
2021-05-09 01:47:03.417 | INFO | dask.settings.development:<module>:47 - Debug is True
2021-05-09 01:47:03.417 | INFO | dask.settings.development:<module>:50 - /home/phillyharper/dask/static
2021-05-09 01:47:03.417 | INFO | dask.settings:<module>:7 - DEV SETTINGS SETTINGS
但是,当我点击页面时 - 没有加载任何静态文件。他们都是404,给这个:~
[09/May/2021 01:47:51] "GET / HTTP/1.1" 200 22203
[09/May/2021 01:47:51] "GET /static/src/assets/lib/perfect-scrollbar/css/perfect-scrollbar.css HTTP/1.1" 404 1800
[09/May/2021 01:47:51] "GET /static/src/assets/lib/material-design-icons/css/material-design-iconic-font.min.css HTTP/1.1" 404 1854
[09/May/2021 01:47:51] "GET /static/src/assets/css/app.css HTTP/1.1" 404 1692
[09/May/2021 01:47:51] "GET /static/src/assets/lib/jquery/jquery.min.js HTTP/1.1" 404 1731
[09/May/2021 01:47:51] "GET /static/src/assets/lib/bootstrap/dist/js/bootstrap.bundle.min.js HTTP/1.1" 404 1794
[09/May/2021 01:47:51] "GET /static/src/assets/lib/perfect-scrollbar/js/perfect-scrollbar.min.js HTTP/1.1" 404 1806
[09/May/2021 01:47:51] "GET /static/src/js/app.js HTTP/1.1" 404 1665
[09/May/2021 01:47:51] "GET /static/src/assets/img/avatar1.png HTTP/1.1" 404 1704
[09/May/2021 01:47:51] "GET /static/src/assets/img/avatar.png HTTP/1.1" 404 1701
[09/May/2021 01:47:51] "GET /static/src/assets/img/avatar2.png HTTP/1.1" 404 1704
[09/May/2021 01:47:51] "GET /static/src/assets/lib/bootstrap/dist/js/bootstrap.bundle.min.js HTTP/1.1" 404 1794
[09/May/2021 01:47:51] "GET /static/src/assets/img/avatar3.png HTTP/1.1" 404 1704
[09/May/2021 01:47:51] "GET /static/src/assets/img/avatar4.png HTTP/1.1" 404 1704
[09/May/2021 01:47:51] "GET /static/src/assets/img/avatar6.png HTTP/1.1" 404 1704
[09/May/2021 01:47:51] "GET /static/src/assets/img/avatar5.png HTTP/1.1" 404 1704
[09/May/2021 01:47:51] "GET /static/src/js/app.js HTTP/1.1" 404 1665
我发现另一个线程说使用 findstatic。我试过了,但它表明 Django 似乎只在我的虚拟环境中寻找静态,而不是在 STATIC_ROOT 的位置尝试。即使 findstatic 没有找到它,我也可以确认下面的 css 文件确实存在。
/home/phillyharper/dask/static/src/assets/css/app.css
>> python manage.py findstatic --verbosity 2 src/assets/css/app.css
No matching file found for 'src/assets/css/app.css'.
Looking in the following locations:
/home/phillyharper/.cache/pypoetry/virtualenvs/django_signally-JCl81_Vu-py3.8/lib/python3.8/site-packages/django/contrib/admin/static
/home/phillyharper/.cache/pypoetry/virtualenvs/django_signally-JCl81_Vu-py3.8/lib/python3.8/site-packages/django_extensions/static
这是我的 django 项目结构,在 WSL2 上运行。
.
├── certbot
│ └── conf
│ ├── accounts
│ ├── csr
│ └── keys
├── dask
│ ├── apps
│ │ ├── core
│ │ ├── ethscan
│ │ ├── pricegrabber
│ │ ├── scraping
│ │ ├── trader
│ │ └── twitter
│ └── dask
│ ├── __pycache__
│ └── settings
├── nginx
│ └── user_conf.d
└── static
└── src
├── assets
├── html
├── js
└── sass
【问题讨论】:
***.com/questions/66437690/… 【参考方案1】:STATIC_ROOT 仅用于生产。在开发期间提供静态服务时,您需要 STATICFILES_DIR = ["list of directory to look through"]
这可能是有原因的......
【讨论】:
文档没有这样说。以上是关于Django 404 上的静态文件,同时设置了 static_root 和 static_url的主要内容,如果未能解决你的问题,请参考以下文章