簡體   English   中英

提供靜態文件django 1.9生產錯誤

[英]Serving static files django 1.9 production Error

我已經嘗試在其他堆棧溢出帖子中關於在生產中提供Django靜態文件的解決方案,但我無法讓它們工作。 我的靜態文件位於我的django app文件夾(聯盟)中。

我的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上檢查源文件並單擊指向css文件的鏈接時,它們不起作用。 所以我猜測到css文件的鏈接不起作用?

加載css和javascript文件的鏈接和腳本是:

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

謝謝!

好的,這是一個非常微不足道的問題。 你需要做兩件事。

首先,當您使用本地python django開發服務器時 ,只需在app文件夾中查找靜態文件即可提供靜態文件,如果您在STATIC_DIRS中為靜態文件指定了另一個文件夾,那么它也將查看。

但是如果您在其他服務器上提供文件,例如NginxHerkouApache ,那么您需要收集Apache將從中讀取靜態文件的另一個目錄中的所有靜態文件。

要實現上述目標,您需要執行python manage.py collectstatic

一旦執行此操作,所有靜態文件將收集在settings.py中 STATIC_ROOT值指定的文件夾中

其次,如果您的STATIC_ROOT以及您的STATIC_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中的值不能相同

有關更好的解釋,請參閱此文章 - > STATICFILES_DIR,STATIC_ROOT和MEDIA_ROOT之間的差異

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM