提供静态文件 django 1.9 生产错误

Serving static files django 1.9 production Error

我已经尝试按照其他堆栈溢出帖子中关于在生产中提供 Django 静态文件的解决方案,但我无法让它们工作。我的静态文件位于我的 django 应用程序文件夹(联盟)中。

我的 CSS 文件没有正确加载;我在 chrome 的控制台中收到以下错误:

Resource interpreted as Stylesheet but transferred with MIME type text/html
Uncaught SyntaxError: Unexpected token !
profile:5 Uncaught SyntaxError: Unexpected token !

我认为 css 文件被解释为 html 文件?我认为 javascript 文件也没有正确加载...

当我检查 chrome 上的源文件并单击 link 到 css 文件时,它们不起作用。所以我猜 link 到 css 文件不起作用?

用于加载 css 和 javascript 文件的 link 和脚本是:

    <head>
<!-- Latest compiled and minified CSS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="{% static 'league/css/bootstrap-theme.css' %}">
<link rel="stylesheet" href="{% static 'league/css/bootstrap-theme.min.css' %}">
<link rel="stylesheet" href="{% static 'league/css/bootstrap.css' %}">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->

<script type="text/javascript" src="{% static 'league/js/bootstrap.min.js' %}"></script>
<script type="text/javascript" src="{% static 'league/js/npm.js' %}"></script>
<!-- jQuery library -->

<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="{% static 'league/css/f-style.css' %}">
</head>

我的settings.py文件中的相关文件如下:

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

STATIC_URL = '/league/static/'
SITE_ID = 1
STATIC_ROOT = BASE_DIR + '/league/static/'

我的apache服务器上的配置文件如下:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    Alias /static /home/ubunu/project/league/static
    <Directory /home/ubuntu/project/league/static>
            Require all granted
    </Directory>
    <Directory /home/ubuntu/project/fantasy>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>

    WSGIDaemonProcess project python-path=/home/ubuntu/project:/home/ubuntu/project/myprojectenv/lib/python2.7/$
    WSGIProcessGroup project
    WSGIScriptAlias / /home/ubuntu/project/fantasy/wsgi.py

</VirtualHost>

谢谢!

好的,这是一个非常微不足道的问题。您需要做两件事。

首先,当您使用 local python django 开发服务器 时,您只需在应用程序文件夹中查找静态文件即可提供静态文件,并且如果您在 STATIC_DIRS 中为您的静态文件指定了另一个文件夹,那么它也会调查那个。

但是如果您在某些 其他服务器 上提供您的文件,例如 NginxHerkouApache 在您的情况下,那么您需要将所有静态文件收集到 Apache 将从中读取静态文件的另一个目录中。

要实现上述目标,您需要做 python manage.py collectstatic

执行此操作后,所有静态文件将收集到 settings.py STATIC_ROOT 值指定的文件夹中

其次,如果您的 STATIC_ROOTSTATIC_URL = '/league/static/' 从技术上讲,事情将无法正常进行,因为这两者的目的不同。

我建议,在 BASE 目录级别创建一个新文件夹,例如 static_root_content 并更改您的 STATIC_ROOT = os.path.join(BASE_DIR, 'static_root_content')

现在,当您 运行 python manage.py collectstatic 您的所有静态数据都将收集到此文件夹中,Apache 将从中查找静态文件。

另外 STATIC_DIRS 和 STATIC_ROOT 在您的 settings.py

中不能有相同的值

查看这篇文章以获得更好的解释 -> Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT