Django入門
HTTP協(xié)議
HTTP協(xié)議是Hyper Text Transfer Protocol(超文本傳輸協(xié)議)的縮寫,是用于萬維網(wǎng)(WWW:World Wide Web )服務(wù)器與本地瀏覽器之間傳輸超文本的傳送協(xié)議糠聪。
http協(xié)議的特性
- 基于tcp/ip協(xié)議之上的應(yīng)用層協(xié)議
- 基于請求-響應(yīng)模式
請求是先由客戶端發(fā)出,服務(wù)端響應(yīng)并返回肝劲,服務(wù)端在沒有收到請求的情況下不好發(fā)送響應(yīng)
- 無狀態(tài)保存
HTTP協(xié)議不保存狀態(tài),自身不對請求和響應(yīng)之間的通信狀態(tài)進(jìn)行保存悔捶。也就是說,協(xié)議對發(fā)送的請求和響應(yīng)都不做持久化處理。
但是很多網(wǎng)站當(dāng)前頁面跳轉(zhuǎn)別的頁面之后仍需要保持登錄狀態(tài),這是就引入了cookie技術(shù)践美,有了cookie再用http協(xié)議通信就可以管理狀態(tài)了
- 無連接
無連接的意思是限制每次連接只處理一個(gè)請求劳殖。服務(wù)端處理完請求就即刻斷開連接,這種方式可以節(jié)約傳輸時(shí)間拨脉。
http請求協(xié)議與響應(yīng)協(xié)議
http協(xié)議包含瀏覽器發(fā)送數(shù)據(jù)給服務(wù)器所需的請求協(xié)議與服務(wù)器發(fā)送數(shù)據(jù)到瀏覽器的請求協(xié)議。
請求端(客戶端)的hppt報(bào)文稱為請求報(bào)文宣增,響應(yīng)端(服務(wù)器端)的稱為響應(yīng)報(bào)文
# 請求首行
# GET / HTTP/1.1\r\n
# # 請求頭
# Host: 127.0.0.1:8001\r\n
# Connection: keep-alive\r\n
# Cache-Control: max-age=0\r\n
# Upgrade-Insecure-Requests: 1\r\n
# User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36\r\n
# Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n
# Accept-Encoding: gzip, deflate, br\r\nAccept-Language: zh-CN,zh;q=0.9\r\n\r\n'
# # 請求體(get請求玫膀,請求體為空)
POST請求
# 請求首行
POST /?name=lqz&age=18 HTTP/1.1\r\n
# 請求頭
Host: 127.0.0.1:8008\r\nConnection: keep-alive\r\nContent-Length: 21\r\nCache-Control: max-age=0\r\nOrigin: http://127.0.0.1:8008\r\nUpgrade-Insecure-Requests: 1\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\nReferer: http://127.0.0.1:8008/?name=lqz&age=18\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: zh-CN,zh;q=0.9\r\nCookie: csrftoken=7xx6BxQDJ6KB0PM7qS8uTA892ACtooNbnnF4LDwlYk1Y7S7nTS81FBqwruizHsxF\r\n\r\n
# 請求體
name=abc&password=123'
![下載.png](https://upload-images.jianshu.io/upload_images/1324730-196d54600f221a79.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![下載.jpg](https://upload-images.jianshu.io/upload_images/1324730-4bba9f3b1d9590d0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#### 請求方式:get與post請求
* GET提交的數(shù)據(jù)會放在URL后,以爹脾?分割URL和傳輸?shù)臄?shù)據(jù)帖旨,參數(shù)之間用&相連。POST是把提交的數(shù)據(jù)放在HTTP包的請求體中
* GET提交的數(shù)據(jù)大小有限制(URL長度限制)灵妨,POST提交的數(shù)據(jù)沒有限制
* GET與POST請求在服務(wù)端獲取請求數(shù)據(jù)方式不同解阅。
#### 響應(yīng)狀態(tài)碼
![響應(yīng)碼.png](https://upload-images.jianshu.io/upload_images/1324730-3f8107386f085008.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
### URL簡介
統(tǒng)一資源定位符是互聯(lián)網(wǎng)上標(biāo)準(zhǔn)資源的地址,互聯(lián)網(wǎng)上的每個(gè)文件都有一個(gè)唯一的URL泌霍。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">協(xié)議://IP:端口(80)/路徑?name=abc&age=123
货抄?之前的是請求路徑,朱转?之后的是請求數(shù)據(jù)部分</pre>
## Django框架
### 一蟹地、django簡介
djangon使用的是MTV模式他與MVC模式本質(zhì)相同,只是定義上有點(diǎn)不同藤为。
#### MVC
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">MVC就是將應(yīng)用分為模型(M)怪与,視圖(V),控制器(C)三層,他們之間以一種插件式缅疟、松耦合的方式連接在一起分别,模型(M)負(fù)責(zé)業(yè)務(wù)對象與數(shù)據(jù)庫的映射(ORM),視圖(V)賦值與用戶的交互存淫,控制器接受用戶的輸入</pre>
![MVC.png](https://upload-images.jianshu.io/upload_images/1324730-e075f242339d04d6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#### MTV
* 模型(Model):負(fù)責(zé)業(yè)務(wù)對象和數(shù)據(jù)庫的關(guān)系映射(ORM).
* 模板(Template):負(fù)責(zé)如何把頁面展示給用戶(html)
* 視圖(View):負(fù)責(zé)業(yè)務(wù)邏輯耘斩,并在適當(dāng)時(shí)候調(diào)用Model和Template
除了以上三層之外,還需要一個(gè)URL分發(fā)器纫雁,它的作用是將一個(gè)個(gè)URL的頁面請求分發(fā)給不同的View處理煌往,View再調(diào)用相應(yīng)的Model和Template,MTV的響應(yīng)模式如下所示:
![MTV+URL.png](https://upload-images.jianshu.io/upload_images/1324730-f7e1fae024b157c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">用戶通過瀏覽器向服務(wù)器發(fā)起一個(gè)請求(request),這個(gè)請求訪問視圖函數(shù)(如果不涉及數(shù)據(jù)調(diào)用轧邪,視圖函數(shù)返回一個(gè)模板)刽脖,視圖函數(shù)調(diào)用模型,模型去數(shù)據(jù)庫查找數(shù)據(jù)忌愚,如何逐級返回曲管,視圖函數(shù)把返回的數(shù)據(jù)填充到模板空格中,最后返回頁面給用戶硕糊。</pre>
### 二院水、Django安裝
##### 1.安裝
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n58" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">方式一:在命令行輸入:pip3 install django
?
pip install django==1.11.9 -i http://pypi.hustunique.org/simple 指定版本號腊徙,指定國內(nèi)鏡像
?
方式二:使用pycharm</pre>
##### 2.創(chuàng)建一個(gè)django project
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n60" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">命令創(chuàng)建:django-admin.py startproject mysite
創(chuàng)建app:python3 manage.py startapp app01</pre>
##### 3.文件目錄介紹
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="true" cid="n62" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">-manage.py---項(xiàng)目入口,執(zhí)行一些命令
-項(xiàng)目名
-settings:全局配置信息
-urls:總路由,請求地址跟視圖函數(shù)的映射關(guān)系
-app名字
-migrations:數(shù)據(jù)庫遷移的記錄
-models.py 數(shù)據(jù)庫表模型
-views 視圖函數(shù)</pre>