Django學習之 基礎部分

2022-07-19 04:15:15 字數 2073 閱讀 7263

學習記錄參考:

講師部落格:

老男孩部落格:

運維平台參考:2561410/1123127

web框架簡寫與解釋

解釋:web應用框架有助於減輕網頁開發時共通性活動的工作負荷,例如許多框架提供資料庫訪問介面、標準樣板以及會話管理等,可提公升**的可再用性。

mvc框架 == model    view      controller

資料庫    模板檔案    業務處理

mtv框架 == model    template     view

資料庫    模板檔案    業務處理

django使用的就是mtv框架。

django

安裝:pip3 install django

3:靜態檔案和模板檔案的配置

模板檔案:

1:在專案內建立templates資料夾

2:將html頁面檔案放到此資料夾內

3:修改settings中在templates修改:'dirs': [os.path.join(base_dir,'templates')],

4:views中的業務**就可以直接呼叫html頁面

如:return render(request,'login.html')

靜態檔案:

1:在專案內建立static資料夾

2:將css,js檔案放置到此資料夾內

3:修改settings最後增加

staticfiles_dirs = (

os.path.join(base_dir,'static'), #注意要加逗號

)4:靜態檔案在html中的引入:

特殊的模板語言

-- }

-- } 可以進行if巢狀

###索引###

如:return render(request,'home.html',})

以上返回值在html中取值方法;

}} 值為列表的獲取

}} 值為字典的獲取

}####條件##### 支援巢狀

如:return render(request,'home.html',})

條件判斷方法:

為真...

為假例項1:使用者登入認證並重定向到新頁面見:login,home

url(r'^login',views.login),

url(r'^home',views.home),

view**如下:

def

login(request):

#獲取使用者提交的方法

#request 包含了客戶端使用者發來的所有資訊

error_msg = ''

if request.method == '

post':

#獲取使用者通過post提交過來的資料

#request.post 方法封裝了所有使用者發過來的請求

#uname = request.post['username']

#upwd = request.post['pwd']

#最好用get方法獲取資料,即使沒有也不會報錯

uname = request.post.get('

username

',none)

upwd = request.post.get('

pwd'

,none)

if uname == '

root

'and upwd == '

root':

#return httpresponse('登入成功')

return redirect('

/home')

elif

not uname or

notupwd:

error_msg = '

使用者名稱密碼不允許為空

'else

:

#使用者名稱密碼不匹配

error_msg = '

使用者名稱或密碼錯誤

'return render(request,'

login.html

',)

view code

django基礎學習之restful api

新建django專案,選擇已存在的django檔案,在新視窗中開啟,設定埠。編寫的api需要認證,使用postman進行除錯。處理queryset資料型別 先進入終端目錄除錯 執行 python manage.py shell命令,實驗資料提取形式 from face.models import r...

django基礎學習

1.進入虛擬環境 workon python3 2.django admin startproject dir name建立專案資料夾 專案配置及設定檔案 3.cd進入專案資料夾 4.python manage.py startap dir name 應用資料夾 5.使用pycharme開啟專案資料...

Python基礎學習 Django基礎

總結一下今天的學習 1,使用黑視窗構建django專案失敗,至今未找到原因 2,使用django結合eclipse構建了乙個簡單的helloworld 介面 3,在模板檔案html檔案裡面編寫 顯示views中傳來的字串 字典 物件 方法 列表等 搭建過程 見笑了 開啟eclipse,安裝環境 新建...