django製作網頁(5)加載靜態文件

lesson41

一、基礎知識:django標準庫contrib:

1、Django的標準庫存放在 django.contrib 包中。每個子包都是一個獨立的附加功能包。 這些子包一般是互相獨立的,不過有些django.contrib子包需要依賴其他子包。

2、在 django.contrib 中對函數的類型並沒有強制要求 。其中一些包中帶有模型(因此需要你在數據庫中安裝對應的數據表),但其它一些由獨立的中間件及模板標籤組成。

3、django.contrib 開發包共有的特性是: 就算你將整個django.contrib開發包刪除,你依然可以使用 Django 的基礎功能而不會遇到任何問題。

4、當 Django 開發者向框架增加新功能的時,他們會嚴格根據這一原則來決定是否把新功能放入django.contrib中。

5、標準庫contrib子包如下:

  • admin : 自動化的站點管理工具。
  • admindocs : 爲Django admin站點提供自動文檔。
  • auth : Django的用戶驗證框架。https://docs.djangoproject.com/en/1.11/ref/contrib/auth/
  • comments : 一個評論應用,關於這個應用的更多信息請參見Django的官方網站。
  • contenttypes : 這是一個用於引入文檔類型的框架,每個安裝的Django模塊作爲一種獨立的文檔類型。 這個框架主要在Django內部被其他應用使用,它主要面向Django的高級開發者。可以通過閱讀源碼來了解關於這個框架的更多信息,源碼的位置在 django/contrib/contenttypes/。
  • csrf : 這個模塊用來防禦跨站請求僞造(CSRF)。
  • databrowse:幫助你瀏覽數據的Django應用。
  • flatpages : 一個在數據庫中管理單一HTML內容的模塊。
  • formtools : 一些列處理表單通用模式的高級庫。
  • gis : 爲Django提供GIS(Geographic Information Systems)支持的擴展。 舉個例子,它允許你的Django模型保存地理學數據並執行地理學查詢。請參看http://geodjango.org/上的文檔。
  • humanize : 一系列 Django 模塊過濾器,用於增加數據的人性化。
  • localflavor:針對不同國家和文化的混雜代碼段。例如,它包含了驗證美國的郵編 以及愛爾蘭的身份證號的方法。
  • markup : 一系列的 Django 模板過濾器,用於實現一些常用標記語言。
  • redirects : 用來管理重定向的框架。
  • sessions : Django 的會話框架。
  • sitemaps : 用來生成網站地圖的 XML 文件的框架。
  • sites : 一個讓你可以在同一個數據庫與 Django 安裝中管理多個網站的框架。
  • syndication : 一個用 RSS 和 Atom 來生成聚合訂閱源的的框架。
  • webdesign : 對設計者非常有用的Django擴展。到編寫此文時,它只包含一個模板標籤{% lorem %}。
  • staticfiles:

    The staticfiles app

    django.contrib.staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.

    https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/

    Settings

    See staticfiles settings for details on the following settings:

STATIC_ROOT

STATIC_URL

STATICFILES_DIRS

STATICFILES_STORAGE

STATICFILES_FINDERS

……

 

二、django項目setting.py中:

(1)INSTALLED_APPS項中增加:django.contrib.staticfiles。

(2)INSTALLED_APPS項中增加:自增加的app名稱。例:項目中已自定義APP,名稱:r_fin。

(3)文件最下一行設置:STATIC_URL = '/static/'

在瀏覽器中請求靜態文件的url。例:127.0.0.1/static/1.jpg。加上此行語句,系統將在項目下的每個app中的static文件中去尋找匹配的文件。

https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/

STATIC_URL:

Default: None

URL to use when referring to static files located in STATIC_ROOT.

Example: "/static/" or "http://static.example.com/"

If not None, this will be used as the base path for asset definitions (the Media class) and the staticfiles app.

It must end in a slash if set to a non-empty value.

You may need to configure these files to be served in development and will definitely need to do so in production.

對應相關操作:app下添加static文件夾

通過python manage.py startapp [app名稱]命令,新建django項目app,每一個app並未默認自動生成static文件夾,需要到每個app文件夾下手動添加。

並且,由於多個app下static文件夾下可能存在的同名文件,造成STATIC_URL變量無法區分。需要在static文件夾下新建一個與本app同名的(建議“同名”,並非強制)子文件夾。例:app名爲r_fin,在其下面新建“static/r_fin”文件夾,該app的一個1.jpg靜態文件存放路徑爲:static/r_fin/1.jpg。

(4)添加STATICFILES_DIRS變量

一些靜態文件不隸屬項目中任何app,在setting.py文件中添加STATICFILES_DIRS變量(或稱屬性),以後DTL就會在這個列表的路徑中查找靜態文件。

例:在項目級別目錄下,新建一個與項目的app同級別目錄名稱爲“static”文件夾(當然也可取別的名稱,參數值也做修改)。

STATICFILES_DIRS=[os.path.join(BASE_DIR,"static"]

小技巧:通過此屬性,不在各app中新建static文件夾,直接在此static下新建以對應app同名的子文件夾“static/app名稱”,將對應靜態文件放入。此種方法便於靜態文件集中管理,提高效率

STATICFILES_DIRS說明:

Default: [] (Empty list)

This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

This should be set to a list of strings that contain full paths to your additional files directory(ies) e.g.:

settings.Py文件添加內容如下:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'r_fin',


STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]


STATIC_URL = '/static/'

三、加載靜態文件其他操作

(5)模板中使用load加載static標籤。

static不是django內置標籤,所以需要在每個模板中都要用load命令加載static標籤。

否則,將提示錯誤:Invalid block tag on line 7 : 'static'. Did you forget to register or load this tag?

例:在項目static文件夾下先建一個css文件。

body{
    background-color: lightblue;
    #background-image: url('2.png')

}

例:然後在項目templates文件夾下建立一個示例html文件。

在模板1.html中加載static標籤,加載項目static文件夾中的style.css文件(app/static文件夾中文件)。

{%load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'indexsjpa.css' %}">
</head>
<body>
<img  width=194 height=194, src="{% static 'a.jpg' %}" alt="">
#img 後邊不允許加逗號
</body>
</html>

 

(6)將static標籤註冊爲內置django內置標籤

如果不想每次在模板中都使用load加載static標籤。可在settings.py中的templates/options添加"builtins":['django.templatetags.static'],這樣模板中可直接使用static標籤,不再用手動load。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'builtins':[
                'django.templatetags.static'
            ]

        },
    },
]

(7)如果沒有在settings.py中的INSTALLED_APPS添加'django.contrib.staticfiles',需要手動將請求靜態文件的url與靜態文件路徑進行映射。代碼如下:

from django.conf import settings

from django.conf.urls.static import static

注:有時頁面內容已經修改,用F5不能反映頁面修改內容,用ctrl+shift+r, 或者ctrl+f5, 強制刷新頁面後即可。

django的templatetags - 曾春雲 - 博客園  https://www.cnblogs.com/zengchunyun/p/6861293.html

Custom template tags and filters | Django documentation | Django  https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/

淺談Django自定義模板標籤template_tags的用處_python_腳本之家  https://www.jb51.net/article/131049.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章