2016-05-14 102 views
0

我想解決這個問題2個小時了。我嘗試了所有閱讀每篇文章的內容,但我認爲我仍然錯過了一些東西。我的靜態文件沒有加載。當我點擊瀏覽器視圖源頁面上的靜態文件url路徑時,出現此錯誤。 「發生服務器錯誤,請與管理員聯繫。」在這個錯誤之前在這個索引頁面中沒問題; 127.0.0.1:8000/但是當我嘗試繼續使用127.0.0.1:8000/home時,它仍然是相同的索引頁面,但它並沒有加載靜態文件,因爲它正在home /目錄中查找靜態文件。所以我決定讓我的靜態網址動態。我雖然沒有犯錯,但是Pycharm說未解決的模板引用是這樣的;Python 2.7 Django 1.9靜態文件管理錯誤

<script src="{% static 'jquery.min.js' %}"></script> 
<script src="{% static 'bootstrap.min.js' %}"></script> 
<script src="{% static 'retina-1.1.0.js' %}"></script> 
<script src="{% static 'jquery.hoverdir.js' %}"></script> 
<script src="{% static 'jquery.hoverex.min.js' %}"></script> 
<script src="{% static 'jquery.prettyPhoto.js' %}"></script> 
<script src="{% static 'jquery.isotope.min.js' %}"></script> 
<script src="{% static 'custom.js' %}"></script> 

在頁面的開頭我用

{% load static from staticfiles %} 

這個和平的代碼。如何解決這些問題,我需要你的幫助!

調試=真

urls.py

from django.conf.urls import url, include 
from . import views 
from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    url(r'^home/$', views.index, name = 'home'), 
    url(r'^$', views.index, name= 'index'), 
    url(r'^second/$', views.second, name = 'second'), 


] 

if settings.DEBUG: 
    urlpatterns +=[ 
     url(r'^static/(?P<path>.*)$', 'django.views.static.serve', 
    {'document_root', settings.STATIC_ROOT} 
), 
    ] 

Settings.py

STATIC_URL = '/static/' 

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'basic/static'), 
) 


STATIC_ROOT = os.path.join(BASE_DIR, 'assests/') 
+0

你絕對肯定調試設置爲true?您應該得到廣泛的錯誤追蹤,而不是「發生服務器錯誤,請與管理員聯繫。」 – Nirri

+0

是的,我確定調試設置爲真 –

+0

你可以從你的'urlpatterns'中刪除除第一個URL之外的所有內容,然後嘗試運行你的應用(保留靜態文件url)。只是想確保另一種模式不衝突。 –

回答

0

在Django文檔的1.9 serving static files during development所陳述的方法是使用下面的:

from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    # ... the rest of your URLconf goes here ... 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

隨着你的模板如下:

{% load staticfiles %} 
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/> 

您是否嘗試過使用調試服務於靜態文件,而這種方法?

+0

我曾嘗試過,但沒有奏效。感謝您的努力.. –