简体   繁体   中英

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

I have nginx, gunicorn, django running on Ubuntu EC2 instance. The entire site operates fine. Except for the admin. The admin isn't displaying properly. I ran "python manage.py collectstatic" and edited the STATIC_ROOT and STATIC_URL. When I load the admin page, it's ugly, but when I inspect the source, the CSS files are located they should be

<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" />

I can look at the nginx access.log and see the files are getting requested and delivered, but the page does not display properly. It's like the files are being received but not processed. Error log is clean.

SOLVED

Under the console tab in Chrome Developer Tools I noticed the following:

Resource interpreted as Script but transferred with MIME type text/plain: "http://staticfiles.<mydomain>.com/static/admin/js/jquery.min.js". 

So the files were getting delivered to browser, but it didn't know what to do with them. To fix it I had to edit the nginx.conf and specify the default type for a couple directories ...

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/;  
}

That fixed it; the django admin loads the stylesheets and javascript files and looks and operates normally. I hope this helps someone else.

My Solve ;

1.step

settings.py edit

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

run collesctstatic in terminal :)

python manage.py collectstatic

The proper way to solve this is to add include /etc/nginx/mime.types; in the http section of the nginx configuration file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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