2016-01-26 13 views
2
  1. 我已經構建了一個web應用程序(使用Django)。在settings.py爲什麼在Django應用程序中,我的瀏覽器源代碼中的css文件是空的?

  2. STATIC_ROOT=os.path.join(BASE_DIR,"static_in_pro","static_root") 
    STATIC_DIRS=os.path.join(BASE_DIR,"static_in_pro","our_static") 
    STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder", 
    "django.contrib.staticfiles.finders.AppDirectoriesFinder"] 
    
  3. 在我的模板base.html

    link href="{% static 'css/main.css'%}" rel="stylesheet" 
    link href="{% static 'css/navbar-static-top.css'%}" rel="stylesheet" 
    
  4. 當我使用 「collectstatic」,所有靜態文件正確複製。

  5. 但是當我runserver,它不應用靜態,實際上瀏覽器中的main.css是空的! 有什麼問題?
+0

您是否嘗試將完整的應用程序路徑添加到模板中?我的意思是App/Static/Templates/blabla – NeoVe

+0

for DIRS?no。我應該如何使用它? – fatemeh

+0

哎呀,它在「C:\ Python34 \ Lib \ site-packages \ django \ contrib \ admin \ static \ admin \ admin \ admin \ css」中查找靜態:(。問題是什麼? – fatemeh

回答

0

靜態文件旨在由網絡服務器提供服務(NGINX非常適用於此)。所以在開發時,Django的開發服務器必須爲你服務,因爲沒有真正的Web服務器。但是,在默認情況下Django不這樣做,所以你必須將代碼添加到您的URL配置是這樣的:

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) 

查看更多在docs:Managing static files during development

+0

謝謝。我已經將它添加到我的urls.py. – fatemeh

+0

它現在工作嗎? – Nikita

相關問題