缺少 Django Grappelli 样式 [Digital Ocean Setup]

Django Grappelli style is missing [Digital Ocean Setup]

我部署了一个安装了 Django admin 的 digital ocean droplet。当我安装 Django Grappelli 并尝试加载管理员时,Django-grappelli 中的 css 和 js 文件丢失,导致页面显示 Plain HTML。当我检查元素时,所有必要的 grappelli 文件都丢失了,代码为 404。

有什么想法吗??

以下是我的 Settings.py 仅供参考:

"""
Django settings for django_project project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxx'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['xxxxxxxxxx']


# Application definition

INSTALLED_APPS = (
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    'django.contrib.auth.context_processors.auth',

)

ROOT_URLCONF = 'django_project.urls'

WSGI_APPLICATION = 'django_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django',
        'USER': 'django',
        'PASSWORD': 'Wxxxxx',
        'HOST': 'localhost',
        'PORT': '',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

这是我的 NGINX 配置:

    upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/django_project/django_project/static;
    }

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

NGINX 错误日志:

    2015/05/04 08:52:03 [error] 1056#0: *1060 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:03 [error] 1056#0: *1062 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/screen.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/screen.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:03 [error] 1056#0: *1063 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/mueller/grid/output.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/mueller/grid/output.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:03 [error] 1056#0: *1064 open() "/home/django/django_project/django_project/static/grappelli/jquery/jquery-1.9.1.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/jquery-1.9.1.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:03 [error] 1056#0: *1065 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:03 [error] 1056#0: *1066 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:03 [error] 1056#0: *1060 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:03 [error] 1056#0: *1060 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/"
2015/05/04 08:52:09 [error] 1056#0: *1067 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:09 [error] 1056#0: *1067 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/screen.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/screen.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:09 [error] 1056#0: *1069 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/mueller/grid/output.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/mueller/grid/output.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:09 [error] 1056#0: *1071 open() "/home/django/django_project/django_project/static/grappelli/jquery/jquery-1.9.1.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/jquery-1.9.1.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:09 [error] 1056#0: *1070 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:09 [error] 1056#0: *1072 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:09 [error] 1056#0: *1067 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:09 [error] 1056#0: *1067 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/logout/"
2015/05/04 08:52:12 [error] 1056#0: *1067 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:12 [error] 1056#0: *1069 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/screen.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/screen.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:12 [error] 1056#0: *1071 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/mueller/grid/output.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/mueller/grid/output.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:12 [error] 1056#0: *1070 open() "/home/django/django_project/django_project/static/grappelli/jquery/jquery-1.9.1.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/jquery-1.9.1.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:12 [error] 1056#0: *1072 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:12 [error] 1056#0: *1073 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:12 [error] 1056#0: *1067 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:12 [error] 1056#0: *1067 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1076 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1078 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/screen.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/screen.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1076 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/mueller/grid/output.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/mueller/grid/output.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1080 open() "/home/django/django_project/django_project/static/grappelli/jquery/jquery-1.9.1.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/jquery-1.9.1.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1079 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1081 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1076 open() "/home/django/django_project/django_project/static/grappelli/jquery/jquery-1.9.1.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/jquery-1.9.1.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1076 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:52:47 [error] 1056#0: *1076 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:09 [error] 1056#0: *1083 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:09 [error] 1056#0: *1083 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/screen.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/screen.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:09 [error] 1056#0: *1085 open() "/home/django/django_project/django_project/static/grappelli/stylesheets/mueller/grid/output.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/stylesheets/mueller/grid/output.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:09 [error] 1056#0: *1086 open() "/home/django/django_project/django_project/static/grappelli/jquery/jquery-1.9.1.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/jquery-1.9.1.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:10 [error] 1056#0: *1088 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:10 [error] 1056#0: *1089 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:10 [error] 1056#0: *1083 open() "/home/django/django_project/django_project/static/grappelli/jquery/jquery-1.9.1.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/jquery-1.9.1.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:10 [error] 1056#0: *1083 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/js/jquery-ui-1.10.3.custom.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:10 [error] 1056#0: *1083 open() "/home/django/django_project/django_project/static/grappelli/js/grappelli.min.js" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/js/grappelli.min.js HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"
2015/05/04 08:57:14 [error] 1056#0: *1083 open() "/home/django/django_project/django_project/static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css" failed (2: No such file or directory), client: 89.241.185.12, server: _, request: "GET /static/grappelli/jquery/ui/css/custom-theme/jquery-ui-1.10.3.custom.min.css HTTP/1.1", host: "178.62.67.237", referrer: "http://178.62.67.237/admin/login/?next=/admin/"

运行 python manage.py collectstatic
这将收集要由 Web 服务器切断的静态文件。

Deploying static files 的官方文档。

更新

# Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

这似乎是权限问题。将此块删除为已设置的 /static 。 admin css 将被 collectstatic 收集到 /static/admin 目录中。重新加载 nginx。

同时检查 Nginx 日志。

删除:

# Proxy the static assests for the Django Admin panel
location /static/admin {
   alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}

来自你的 nginx conf。它覆盖了 Django 为应用程序查找静态文件的方式并破坏了 Grappelli。

记得之后重新加载nginx。