標籤:base live back 頁面 顏色 ack using files asi
靜態檔案路徑設定官方說明
1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
2. In your settings file, define STATIC_URL, for example:
STATIC_URL = ‘/static/‘
3. Store your static files in a folder called static in your app. For example my_app/static/my_app/myimage.jpg.
4. In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).
{% load staticfiles %}<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
使用執行個體
在前面的例子中,我們使用了上面第三種設定方式,在app目錄下,建立了static目錄,裡面還建立了名字跟app名稱一樣的目錄,裡面才放靜態資源檔案,路徑如:
polls/static/polls/style.css,在template中引入時使用
{% load static %}<link rel="stylesheet" type="text/css" href="{% static ‘polls/style.css‘ %}" />
一、Django 項目預設讀取靜態檔案目錄如 /projectName/appName/static/
二、如果我們不想將靜態檔案放到app甚至project目錄下時,可以在settings.py 中設定 STATICFILES_DIRS ,如:
STATICFILES_DIRS = ( ‘D://static/‘, # os.path.join(BASE_DIR, "static"), # ‘/var/www/static/‘,)
接著前面的例子,此時建立 D://static/polls/style.css
li a { color: red;}body { background: white url("images/background.jpg") no-repeat center Top;}
重新整理頁面 則可發現,連結中的文字顏色由原先的綠色變成了紅色,說明頁面引用的css路徑生效
Django基礎,Day9 - 靜態檔案目錄與路徑設定說明(eg. images, JavaScript, CSS)