部署的 Django 3 项目看不到静态文件
Deployed Django 3 project doesn't sees static files
已部署(在服务器上)Django 3 项目看不到静态和 css 文件(整个静态文件夹),开发机器上的本地运行完美。
我遵循的指南是 this。
- 我用 ubuntu 18.04
建立了一个 Digital 海洋水滴
- 我的静态文件可以在这里找到
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'mymainapp/static/')]
#STATIC_ROOT = os.path.join(BASE_DIR, 'static') # Removed based [on](
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mymainapp/media')
MEDIA_URL = '/media/'
- 比 guide
中所有其他使用 Postgres、Nginx 和 Gunicorn 的员工都要好
- 但静态文件(图像、SVG、css、文件未加载)
- 我已将 bootstrap CSS 文件下载到本地目录
- 该网站在线,如果我在 chrome 中按 F12,它会显示以下错误消息(转动
DEBUG = True
不会再提供任何错误消息或错误页面)
Failed to load resource: the server responded with a status of 404 (Not Found)
homepage03_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
logo_03c_small.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
jquery.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
popper.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
tiktok_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
instagram_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
twitch_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
twitter_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
youtube_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
facebook_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
mobile_features_large_compressed.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
creator_platforms_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
creator_sm_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
reoccuring_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
stripe_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
customerchat.php?app_id=&attribution=setup_tool&channel=https%3A%2F%2Fstaticxx.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D46%23cb%3Df2ca0722231e688%26domain%3D159.65.234.146%26origin%3Dhttp%3A%2F%2F159.65.234.146%2Ff167133a445f9a4%26relation%3Dparent.parent&container_width=0&locale=en_US&page_id=2172253486430236&sdk=joey:1 Failed to load resource: the server responded with a status of 500 ()
paypal_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
googleads1_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
googlepay_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
amazon_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bitcoin_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
logo_02_Background.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
shirts_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
message_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
sopnsor2_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
socialmedia_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
combinedbarnad_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
applepay_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
check.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
feature_description_laptop_cut.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
self_image_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
reddit_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
linkedin_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
collie_2_comp.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
DevTools failed to parse SourceMap: chrome-extension://hnmpcagpplmpfojmgmnngilcnanddlhb/browser-polyfill.min.js.map
DevTools failed to parse SourceMap: chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.preload.js.map
DevTools failed to parse SourceMap: chrome-extension://hnmpcagpplmpfojmgmnngilcnanddlhb/browser-polyfill.min.js.map
DevTools failed to parse SourceMap: chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.postload.js.map
- 我在阅读指南之前遇到了同样的错误,只是有一个基本的拉取 git 版本和 运行
python3 manage.py runserver
它加载了网站而不是 css 和静态元素
- 我已将基础
/media
和 /static
文件夹放入 git 忽略(这些应该是收集元素静态夹头的文件夹)。原来静态在myowndjango-project/myowndjango/static
和myowndjango-project/myowndjango/media
下
- nginx我也设置如下
...
location /static/ {
root /home/myserveruser/myowndjango-project;
}
location /media/ {
root /home/myserveruser/myowndjango-project;
}
...
我尝试解决的问题
- 查找服务器访问日志
- 运行
/var/log/nginx/access.log
结果:-bash: /var/log/nginx/access.log: Permission denied
- 运行
sudo /var/log/nginx/access.log
结果:sudo: /var/log/nginx/access.log: command not found
- 更改 nginx 文件
...
location /static/ {
root /home/myserveruser/myowndjango-project/myowndjango/static;
}
location /media/ {
root /home/myserveruser/myowndjango-project/myowndjango/media;
}
...
- 运行
python3 manage.py collectstatic
(重启了nginx和gunicorn),还是报同样的错误。
您在 nginx 中将文件夹作为静态文件的位置错误,您应该将完整路径放入确切的静态文件/媒体文件文件夹
location /static/ {
root /home/myserveruser/myowndjango-project/myowndjango/static;
}
官方 Django 3 文档中有 3 个有用的资源可以回答这个问题
创建一个 local_settings.py 文件,该文件应该 replace/add 属性到部署模式下的正常设置文件。
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
根据你的情况导入
已部署(在服务器上)Django 3 项目看不到静态和 css 文件(整个静态文件夹),开发机器上的本地运行完美。
我遵循的指南是 this。
- 我用 ubuntu 18.04 建立了一个 Digital 海洋水滴
- 我的静态文件可以在这里找到
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'mymainapp/static/')]
#STATIC_ROOT = os.path.join(BASE_DIR, 'static') # Removed based [on](
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mymainapp/media')
MEDIA_URL = '/media/'
- 比 guide 中所有其他使用 Postgres、Nginx 和 Gunicorn 的员工都要好
- 但静态文件(图像、SVG、css、文件未加载)
- 我已将 bootstrap CSS 文件下载到本地目录
- 该网站在线,如果我在 chrome 中按 F12,它会显示以下错误消息(转动
DEBUG = True
不会再提供任何错误消息或错误页面)
Failed to load resource: the server responded with a status of 404 (Not Found)
homepage03_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
logo_03c_small.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
jquery.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
popper.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
tiktok_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
instagram_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
twitch_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
twitter_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
youtube_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
facebook_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
mobile_features_large_compressed.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
creator_platforms_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
creator_sm_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
reoccuring_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
stripe_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
customerchat.php?app_id=&attribution=setup_tool&channel=https%3A%2F%2Fstaticxx.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D46%23cb%3Df2ca0722231e688%26domain%3D159.65.234.146%26origin%3Dhttp%3A%2F%2F159.65.234.146%2Ff167133a445f9a4%26relation%3Dparent.parent&container_width=0&locale=en_US&page_id=2172253486430236&sdk=joey:1 Failed to load resource: the server responded with a status of 500 ()
paypal_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
googleads1_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
googlepay_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
amazon_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bitcoin_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
logo_02_Background.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
shirts_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
message_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
sopnsor2_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
socialmedia_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
combinedbarnad_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
applepay_f.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
check.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
feature_description_laptop_cut.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
self_image_comp.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)
reddit_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
linkedin_col.svg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
collie_2_comp.jpg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
DevTools failed to parse SourceMap: chrome-extension://hnmpcagpplmpfojmgmnngilcnanddlhb/browser-polyfill.min.js.map
DevTools failed to parse SourceMap: chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.preload.js.map
DevTools failed to parse SourceMap: chrome-extension://hnmpcagpplmpfojmgmnngilcnanddlhb/browser-polyfill.min.js.map
DevTools failed to parse SourceMap: chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.postload.js.map
- 我在阅读指南之前遇到了同样的错误,只是有一个基本的拉取 git 版本和 运行
python3 manage.py runserver
它加载了网站而不是 css 和静态元素 - 我已将基础
/media
和/static
文件夹放入 git 忽略(这些应该是收集元素静态夹头的文件夹)。原来静态在myowndjango-project/myowndjango/static
和myowndjango-project/myowndjango/media
下
- nginx我也设置如下
...
location /static/ {
root /home/myserveruser/myowndjango-project;
}
location /media/ {
root /home/myserveruser/myowndjango-project;
}
...
我尝试解决的问题
- 查找服务器访问日志
- 运行
/var/log/nginx/access.log
结果:-bash: /var/log/nginx/access.log: Permission denied
- 运行
sudo /var/log/nginx/access.log
结果:sudo: /var/log/nginx/access.log: command not found
- 运行
- 更改 nginx 文件
...
location /static/ {
root /home/myserveruser/myowndjango-project/myowndjango/static;
}
location /media/ {
root /home/myserveruser/myowndjango-project/myowndjango/media;
}
...
- 运行
python3 manage.py collectstatic
(重启了nginx和gunicorn),还是报同样的错误。
您在 nginx 中将文件夹作为静态文件的位置错误,您应该将完整路径放入确切的静态文件/媒体文件文件夹
location /static/ {
root /home/myserveruser/myowndjango-project/myowndjango/static;
}
官方 Django 3 文档中有 3 个有用的资源可以回答这个问题
创建一个 local_settings.py 文件,该文件应该 replace/add 属性到部署模式下的正常设置文件。
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
根据你的情况导入