我已經全部通過了文檔,對我來說它沒有意義。我運行了collectstatic,我在我的應用程序和項目目錄中設置了/ static /目錄,我將STATIC_URL和STATIC_ROOT添加到我的settings.py文件中(但我不知道如何知道它們是否設置正確)和{ STATIC_URL}}仍然沒有渲染出任何東西。這一切似乎只是將html連接到css的一大堆矯枉過正。如何在開發中將CSS提供給Django?
我覺得我失去了在細節;任何人都可以提供這個靜態文件想法的高級細分?恐怕我可能對生產和開發設置有不同的說明。
更多:這是從我的settings.py文件的相關位:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'django.contrib.staticfiles',
'dashboard.base',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'C:/Users/Sean/Desktop/Work Items/dashboard/base/static/',
)
這是我想在我的模板的代碼:
<link rel="stylesheet" href="{{ STATIC_URL }}css/960.css" />
確定。我做了每個人都推薦的更改。這裏是我的新urls.py:
from django.conf.urls.defaults import *
from base.views import show_project
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^dashboard/', include('dashboard.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
('^show_project/$', show_project),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True }))
urlpatterns += staticfiles_urlpatterns()
我錯過了什麼嗎?通常我的問題是CS專家認爲理所當然的基本問題,但我錯過了。
謝謝 - 我有處理器 - 你可以在我上面的文件中看到它。不過,您的設置對我有幫助:我在STATIC_ROOT和STATIC_URL設置中使用了絕對路徑。我已經修復了它,但我認爲我仍然需要STATICFILES_DIRS中的絕對路徑,對吧? – StormShadow
@StormShadow,假設你的靜態文件和'settings.py'在同一個目錄下,你可以做'os.path.join(os.dirname(__ file __),「static」)'。 – Geo
我認爲你沒有提到你是否使用Django開發服務器或其他服務器(如nginx或Apache)提供文件。如果第二個問題可能與您的生產服務器的配置有關,而與您項目中的配置設置無關。 – kaysa