我是一個Django新手,在我的第一個項目上工作並遇到靜態文件的問題。Django靜態文件將不會加載
我已經使用由兩個模板組成的django.contrib.auth
創建了一個簡單的認證系統:mysite/templates/index.html
和mysite/templates/registration/login.html
。我有mysite/static
全球靜態內容,我希望能夠訪問所有應用程序呈現的所有模板。
mysite/templates/index.html
包含<img src="{{ STATIC_URL }}pics03.jpg"/>
當我訪問的URL localhost:8000/
mysite/templates/registration/login.html
包含<img src="{{ STATIC_URL }}pics03.jpg"/>
也呈現爲"static/pics03.jpg"
,當我訪問的URL "localhost:8000/accounts/login/"
在我的網址不會加載它呈現爲"static/pics03.jpg"
和加載罰款。 py我有:
urlpatterns = patterns('',
url(r'^$', 'mysite.views.home'), # plays index.html template
url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
在我的settings.py我有:
PROJECT_DIR = os.path.dirname(__file__)
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_DIR,'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_URL = '/static/'
STATIC_ROOT = ''
我是Django的應該尋找在STATICFILES_DIRS全球靜態內容的印象,但即使我在那裏更改URL以一個絕對路徑它沒有找到login.html的靜態內容靜態文件夾。任何人都可以對此有所瞭解嗎?
是的,這是問題..謝謝! – JimJay 2012-04-28 17:41:34
那麼,實際上在後一種情況下,有必要包裝該規則。如果你看看django的代碼,你會發現這個視圖不會檢查DEBUG。 – 2013-02-25 21:57:16
該視圖從未命中,因爲您的網絡服務器設置爲捕獲這些文件並提供靜態文件,因此不需要檢查。 – krs 2013-04-08 02:31:46