Django載入靜態檔案

2021-09-11 17:08:03 字數 1240 閱讀 1189

(1) 安裝python和pip,這裡就不再贅述

(2)   通過pip 安裝命令安裝django,命令為 pip install django

(3)  進入命令列,進入你打算建立專案的位置,我專案安裝位置是"d:/python/django",執行命令"django-admin startproject helloworld",建立乙個helloworld專案在當前目錄之下,你可以在當前目錄下檢視到helloworld資料夾,如果沒有找到該資料夾,可能是你對這個目錄的讀寫許可權不夠。

(4)進入第乙個helloworld目錄之下執行python manage.py runserver 0.0.0.0:8000,在命令介面沒有報錯的情況下,在瀏覽器輸入127.0.0.1:8000,瀏覽器會彈出django的介面,彈出這個介面則表示專案建立成功。

(5)我們在第乙個helloworld目錄之下建立乙個templates資料夾用於存放前端的html檔案,同樣在該目錄下建立乙個static資料夾,用於存放前端呼叫的靜態檔案;然後在templates目錄之下建立乙個index.html檔案,在static目錄之下建立乙個images目錄,然後在該image目錄之下存放了一張desk.png的,使用者前端呼叫。

(6)在第二個helloworld目錄之下建立乙個view.py的檔案,相應錄入的**如下:

from django.shortcuts import render

def hello(request):

content={}

str_name="desk.png"

content['img_dir_args']='''/static/images/'''+str_name

print("content:",content['img_dir_args'])

return render(request,"index.html",content)

(7)修改第二個helloworld目錄之下的的url.py檔案,相應**如下:

from django.conf.urls import url

from .import view

urlpatterns = [

url(r'^index$',view.hello)

](8)修改第二helloworld個目錄下的settings.py,修改的相應**如下:

Django 載入靜態檔案

在乙個網頁中,不僅僅只有乙個html骨架,還需要css樣式檔案,js執行檔案以及一些等。因此在dtl中載入靜態檔案是乙個必須要解決的問題。在dtl中,使用static標籤來載入靜態檔案。要使用static標籤,首先需要。載入靜態檔案的步驟如下 確保在settings.py中設定了static url...

Django 模板載入靜態檔案

2 確保在 settings.py中設定了static url。staticfiles dirs os.path.join base dir,static 5 在模版中使用 load 標籤載入 static 標籤。比如要載入在專案的 static 資料夾下的 style.css 的檔案。那麼示例 如...

Django 中載入靜態檔案 static 詳解

2 確保在settings.py 中設定了static url 上面兩條都是在建立django專案的時候就自動給我們弄好了,只要我們沒有改動它,就不用管。如果不想每次在模板中載入靜態檔案都使用load載入static標籤,那麼可以在settings.py中的templates新增 builtins ...