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