django上的靜態檔案

2021-06-29 13:26:31 字數 1151 閱讀 4026

django裡使用靜態檔案,貌似有這麼幾種辦法:

1. 在setting.py裡面:

python**  

# url prefix for static files.

# example: ""

static_url = '/static/'

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(os.path.dirname(__file__), '../static').replace('\\','/'),  

)  

把靜態檔案所在目錄設到staticfiles_dirs裡。

靜態檔案就可以通過 /static/***這樣的url訪問了。不過缺點是static_url不可以設為空(一定要有個字首)。所以像robots.txt這樣的一定要放在根目錄的檔案就不行了(

2. 將靜態頁面設定為模板,通過direct_to_template函式直接顯示

python**  

from django.views.generic.****** import direct_to_template  

urlpatterns = patterns('',  

(r'^robots\.txt$', direct_to_template, ),  

)  

direct_to_template的說明:

3. 如果靜態頁面非常簡單,也可以通過lambda表示式(匿名函式)直接寫在urlpatterns裡

lambda後面的r,指view的引數request。

django上的靜態檔案

django裡使用靜態檔案,貌似有這麼幾種辦法 1.在setting.py裡面 url prefix for static files.example static url static staticfiles dirs put strings here,like home html static ...

Django靜態檔案

專案中的css js都是靜態檔案。一般會將靜態檔案放到乙個單獨的目錄中,以方便管理。在html頁面中呼叫時,也需要指定靜態檔案的路徑,django中提供了一種解析的方式配置靜態檔案路徑。靜態檔案可以放在專案根目錄下,也可以放在應用的目錄下,由於有些靜態檔案在專案中是通用的,所以推薦放在專案的根目錄下...

Django 靜態檔案

靜態檔案是 頁面所使用到的提前已經寫好的檔案,如css,js,第三方元件 bootstrap,sweetalert,fontawesome等 所使用到的html檔案統一放到templates資料夾中 那針對 所使用到的靜態檔案也應該單獨放到乙個資料夾中儲存,這個資料夾預設情況下都叫static,該資...