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)了
  • 無連接
無連接的意思是限制每次連接只處理一個(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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市檬某,隨后出現(xiàn)的幾起案子撬腾,更是在濱河造成了極大的恐慌,老刑警劉巖恢恼,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件民傻,死亡現(xiàn)場離奇詭異,居然都是意外死亡场斑,警方通過查閱死者的電腦和手機(jī)漓踢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來漏隐,“玉大人喧半,你說我怎么就攤上這事∏嘣穑” “怎么了挺据?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長脖隶。 經(jīng)常有香客問我吴菠,道長,這世上最難降的妖魔是什么浩村? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任做葵,我火速辦了婚禮,結(jié)果婚禮上心墅,老公的妹妹穿的比我還像新娘酿矢。我一直安慰自己,他們只是感情好怎燥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布瘫筐。 她就那樣靜靜地躺著,像睡著了一般铐姚。 火紅的嫁衣襯著肌膚如雪策肝。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天隐绵,我揣著相機(jī)與錄音之众,去河邊找鬼。 笑死依许,一個(gè)胖子當(dāng)著我的面吹牛棺禾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播峭跳,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼膘婶,長吁一口氣:“原來是場噩夢啊……” “哼缺前!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起悬襟,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤衅码,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后脊岳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肆良,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年逸绎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片夭谤。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡棺牧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出朗儒,到底是詐尸還是另有隱情颊乘,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布醉锄,位于F島的核電站乏悄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏恳不。R本人自食惡果不足惜檩小,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望烟勋。 院中可真熱鬧规求,春花似錦、人聲如沸卵惦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沮尿。三九已至丛塌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間畜疾,已是汗流浹背赴邻。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留啡捶,地道東北人乍楚。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像届慈,于是被迫代替她去往敵國和親徒溪。 傳聞我的和親對象是個(gè)殘疾皇子忿偷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容