[英]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.