加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 13:38:45 所属栏目:Python 来源:网络整理
导读:我有在Ubuntu EC2实例上运行的nginx,gunicorn,django.整个网站运作良好.除了管理员.管理员无法正常显示.我运行了“ python manage.py collectstatic”并编辑了STATIC_ROOT和STATIC_URL.当我加载管理页面时,它很难看,但是当我检查源代码时,它们应该是CSS文件
我有在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读