http協(xié)議與django安裝

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)了
  • 無連接
無連接的意思是限制每次連接只處理一個請求。服務(wù)端處理完請求就即刻斷開連接,這種方式可以節(jié)約傳輸時間。

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)上的每個文件都有一個唯一的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)時候調(diào)用Model和Template

除了以上三層之外,還需要一個URL分發(fā)器,它的作用是將一個個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ā)起一個請求(request),這個請求訪問視圖函數(shù)(如果不涉及數(shù)據(jù)調(diào)用,視圖函數(shù)返回一個模板),視圖函數(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)建一個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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容