2009-12-12 35 views
4

我的Django模板使用了很多相關的東西:圖像,樣式表等。放置Django模板的相關文件的位置?

我應該在哪裏放這些文件,或者我應該如何在模板本身中引用它們?

現在我正在使用開發服務器。

我知道這是一件很常見的事情,但我無法弄清楚。

回答

5

我把它們放在一個名爲static的文件夾中,它位於Web項目的頂層文件夾中。

實施例:

/靜態/ IMG/
/靜態/ JS/
/靜態/ CSS/
/模板/
urls.py
settings.py

然後我在我的urls.py文件中有以下規則:

(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), 

settings.py包含:

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static').replace('\\', '/') 
ADMIN_MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static/admin').replace('\\', '/') 
+4

我們稱之爲「媒體」文件夾。 – 2009-12-12 03:18:17

+0

啊!這有點棘手。我試圖命名我的文件夾「媒體」,但它當然與管理員的媒體文件夾衝突。 – shinjin 2009-12-12 11:05:17

+0

shinjin:因此請在設置中將ADMIN_MEDIA_PREFIX設置爲其他名稱,例如/ admin-media / – 2009-12-12 12:58:14

0

我們把我們在/ media。與網站佈局特別相關的所有內容都會進一步分離。當然,這些靜態內容都不是由Django在生產站點上提供的。他們通常甚至不在同一臺物理服務器上。

/media 
    /images - this is for content-specific images 
    /video - these next 2 are normally symlinks to a /big_content folder ... 
    /audio - so that they aren't included in our mercurial repository. 
    /layout - everything that is tied to the basic templates. 
     /css 
     /js 
     /images