繁体   English   中英

Django Admin不工作/丑陋 - 与nginx和gunicorn一起服务

[英]Django Admin not working / ugly - serving with nginx and gunicorn

我有在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" />

我可以查看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". 

所以这些文件正在传递给浏览器,但它不知道如何处理它们。 要修复它,我必须编辑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/;  
}

修好了; django管理员加载样式表和javascript文件,并正常运行。 我希望这有助于其他人。

我的解决;

1.step

settings.py编辑

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
STATIC_ROOT = "/opt/venv/myDjangoProject/static/"
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)

2.step

在终端运行collesctstatic :)

python manage.py collectstatic

解决这个问题的正确方法是添加include /etc/nginx/mime.types; 在nginx配置文件的http部分。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM