2016-08-04 67 views
1

我已經嘗試了其他堆棧溢出文章中關於在生產中服務Django靜態文件的解決方案,但我無法讓他們工作。我的靜態文件位於我的django應用程序文件夾(聯盟)內。服務靜態文件django 1.9生產錯誤

我的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文件正在正確加載...

當我檢查鉻上的源文件,並點擊鏈接到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 [email protected] 
    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> 

謝謝!

+0

沒有你試圖重新啓動服務器的服務器。而在你的模板中,你還沒有使用{%load staticfiles%}標籤。 –

回答

3

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

首先,當您使用Python的Django開發服務器,那麼你的靜態文件只需在應用程序文件夾中查找該staticfiles服務,如果您指定的其他文件夾爲您的靜態文件中STATIC_DIRS ,那麼它也會考慮到這一點。

但如果是一些其他服務器在你的情況下,供應的文件,像Nginx的Herkou阿帕奇,則需要從收集你的所有靜態文件在其他目錄下你的Apache將從中讀取靜態文件。

爲了實現上述需要只要你這樣做,你的所有靜態文件將由STATIC_ROOT值指定的文件夾在你settings.py收集做python manage.py collectstatic

其次,如果您的技術上STATIC_ROOT以及您的STATIC_URL = '/league/static/',事情將不起作用,因爲這兩個用於不同的目的。

我建議,在創建基本目錄級別的新文件夾,說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

+0

我得到一個'GET http://...../static/league/js/index.js 403(Forbidden)'錯誤。當我訪問該網址時,出現以下錯誤頁面:'Forbidden 您無權訪問此服務器上的/static/league/js/prefixfree.min.js.' – alienboy

+0

請參閱 - > http:/ /stackoverflow.com/questions/10873295/error-message-forbidden-you-dont-have-permission-to-access-on-this-server –