我在 Ubuntu EC2 实例上运行了 nginx、gunicorn、django。整个网站运行良好。除了管理员。管理员显示不正确。我运行了“python manage.py collectstatic”并编辑了 STATIC_ROOT 和 STATIC_URL。当我加载管理页面时,它很难看,但是当我检查源代码时,CSS 文件位于它们应该是
<title>Site administration | Django site admin</title>
<link rel="stylesheet" type="text/css" href="http://staticfiles.mydomain.com/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="http://staticfiles.mydomain.com/static/admin/css/dashboard.css" />
Run Code Online (Sandbox Code Playgroud)
我可以查看 nginx access.log 并看到文件正在被请求和传递,但页面无法正确显示。就好像文件正在被接收但没有被处理。错误日志是干净的。
解决了
在 Chrome 开发人员工具的控制台选项卡下,我注意到以下内容:
Resource interpreted as Script but transferred with MIME type text/plain: "http://staticfiles.<mydomain>.com/static/admin/js/jquery.min.js".
Run Code Online (Sandbox Code Playgroud)
所以文件被传送到浏览器,但它不知道如何处理它们。为了修复它,我必须编辑 nginx.conf 并为几个目录指定默认类型......
location /static/admin/js/ {
default_type text/javascript;
alias /home/ubuntu/webapps/<myproject>/static/admin/js/;
}
location /static/admin/css/ {
default_type text/css;
alias /home/ubuntu/webapps/<myproject>/static/admin/css/;
}
Run Code Online (Sandbox Code Playgroud)
那修复了它;django 管理员加载样式表和 javascript 文件并正常查看和运行。我希望这对其他人有帮助。