python - 带有 nginx 静态文件的 Django 无法加载
问题描述
我使用 Debug = True 选项运行 django 服务器。
昨天,我把它改成Debug = False,然后静态文件没有加载。
/etc/nginx/site-enabled/web :
server {
server_name my_domain;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
}
location /static/ {
alias /home/ubuntu/project/static/;
}
}
settings.py 静态设置:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
静态目录路径没有错。我运行服务器python mange.py runserver 0.0.0.0:8000
解决方案
您必须向您的程序宣布静态文件的位置,因此请像这样更改 urls.py 文件:
从 django.conf 导入设置 从 django.conf.urls.static 导入静态
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
然后运行以下命令
python manage.py collectstatic
推荐阅读
- django - Django 中的 ModelAdmin 相关问题
- java - Adobe 体验管理器 6.2
- excel - 解析单个 Excel 单元格并提取信息
- excel - Countifs 使用日期范围、“是”和唯一值
- c++ - is_lock_free() 可以为某些数据类型返回 true 并为另一种数据类型返回 false 吗?
- javascript - System.Windows.Controls.WebBrowser(IFramed 窗口)如何与 xaml(WPF)中的父视图通信
- python - 无法对导入的模块进行monkeypatch
- terminal - Anaconda Nav 未激活环境
- python - ImportError:没有名为 PIL 的模块,即使模块似乎已安装
- django - 我正在尝试使用注册表格为我的用户分配一个组,但收到此错误