简体   繁体   English

Digital Ocean 上的 Django 产品未加载 Static 文件

[英]Static files are not loading for Django product on Digital Ocean

I am about to publish my first Django project on a Digital Ocean droplet.我即将在 Digital Ocean droplet 上发布我的第一个 Django 项目。 I was following the official Digital Ocean guide to install Django on a droplet ( https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#creating-systemd-socket-and-service-files-for-gunicorn ) and everything worked fine but the serving of static files.我正在按照官方数字海洋指南在液滴上安装 Django( https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on -ubuntu-22-04#creating-systemd-socket-and-service-files-for-gunicorn )并且一切正常,但提供 static 个文件。

Here are key parts of my files:以下是我的文件的关键部分:

settings.py设置.py

BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = os.path.join(BASE_DIR,"static")

STATIC_URL = '/static/'

DEBUG = False

COMPRESS_ROOT = os.path.join(BASE_DIR,"static")

COMPRESS_ENABLED = True

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
]

urls.py网址.py

urlpatterns = [
    ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Here is my folder structure on digital ocean:这是我在数字海洋上的文件夹结构:

root/
    snap/
    gymtracker/
        gymenv/
        gymtracker/
            gymtrackerapp/
            static/

I see on in the terminal on digital ocean that all static files are in this location:我在数字海洋的终端上看到所有 static 文件都在这个位置:

/root/gymtracker/gymtracker/static/ /root/gymtracker/gymtracker/静态/

but when I load the page I get 403 for the static files ( the page loads fine otherwise):但是当我加载页面时,我得到 static 文件的 403(否则页面加载正常):

Request Method: GET请求方式:GET

Status Code: 403 Forbidden状态码:403 禁止访问

Thanks so much for your help!非常感谢你的帮助!

Found the solution.找到了解决方案。

The Digital Ocean tutorial ( https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#creating-systemd-socket-and-service-files-for-gunicorn ) missed one important Nginx config change at the end to make your static resources show up.数字海洋教程 ( https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#creating-systemd -socket-and-service-files-for-gunicorn ) 最后错过了一项重要的 Nginx 配置更改,以使您的 static 资源显示出来。

After you follow the tutorial you need to edit /etc/nginx/nginx.conf and change the user from www-data to your username (eg sammy) by running按照教程进行操作后,您需要编辑 /etc/nginx/nginx.conf 并将用户从 www-data 更改为您的用户名(例如 sammy),方法是运行

sudo nano /etc/nginx/nginx.conf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在我的 Django 项目中从 Digital Ocean Spaces 加载 static 文件时遇到问题 - Trouble loading static files from Digital Ocean Spaces in my Django project 在 Nginx + 数字海洋部署中加载 static 文件时遇到问题 - Trouble loading static files in Nginx + Digital Ocean Deployment Digital Ocean,Django和Nginx找不到所有静态文件 - Digital Ocean, Django and Nginx not finding all static files Nginx 无法为 django static 或数字海洋上的媒体文件提供服务 - Nginx failing to serve django static or media files on digital ocean 使用 Digital Ocean 部署 Django 项目时所有静态文件返回 404 错误 - All static files returning 404 error when deploying Django Project using Digital Ocean Django在Digital Ocean上 - Django on Digital Ocean Nginx Digital Ocean 上的 Django 静态文件夹被禁止(403) - Django Static Folder Forbidden (403) on Nginx Digital Ocean 无法为部署到数字海洋生产的 Django 中的 static 文件提供服务 - Unable to serve static file in Django deployed to production in Digital ocean 无法在数字海洋上上传媒体文件 django - Can't upload media files django on digital ocean 我可以使用 django 为用户上传的文件使用数字海洋空间吗? - Can I use digital ocean spaces for user uploaded files with django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM