Python 板


LINE

先來看看csrf_token做了什麼事 django.template.defaulttags.csrf_token #1 http://imgur.com/zF9wpKH django.template.defaulttags.CsrfTokenNode.render #2 http://imgur.com/wFTXzR1 注意這邊的context.get('csrf_token') 也就是說context裡面要有'csrf_token' csrf_token template tag才會有東西 那context裡面的csrf_token從何而來? 如果你有稍微看過settings.py的話 應該會在TEMPLATES裡面發現context_processors 這些context processor會自動加東西在context 所以csrf_token應該也是利用context processor加進去的 然後你認真翻了一下source code發現 在django.template.context_processors有個csrf #3 http://imgur.com/o64Vp1n 這邊他確實產生了csrf_token 但是settings.py裡面卻沒有這個context processor 於是你更認真翻source code 發現django.template.context定義了 _builtin_context_processors = ('django.template.context_processors.csrf',) #4 http://imgur.com/lQhUVv3 而這個東西被用在django.template.engine.Engine.template_context_processors #5 http://imgur.com/LbcOIq1 到這邊我們了解csrf context processor是自動加進去的 而上面這些和你的問題有什麼關係? ※ 引述《left (881 forever)》之銘言: : template = get_template('posting.html') django.template.loader.get_template #6 http://imgur.com/mixZLEu 他會return django.template.backends.django.DjangoTemplates.get_template() 也就是django.template.backends.django.Template #7 http://imgur.com/KOFVrUx 這邊跳了幾步 不過不影響 : request_context = RequestContext(request) : request_context.push(locals()) : html = template.render(request_context) 這邊call上圖的Template.render 然後是django.template.context.make_context #8 http://imgur.com/jql8ohU 注意傳進去的參數 request=None 使得回傳的是django.template.context.Context 而不是RequestContext 差別在於#7執行到self.template.render(context) 跳到django.template.base.Template.render #9 http://imgur.com/tqg1vbf context.bind_template(self) 比較一下Context和RequestContext的bind_template Context: #10 http://imgur.com/M8T1kn4 RequestContext: #11 http://imgur.com/KysHULW 有沒有看到一個熟悉的東西 template.engine.template_context_processors 剛才我們知道這裡面有csrf context processor 所以當傳入django.template.base.Template.render的context是Context的時候 就不會有csrf_token : return HttpResponse(html) : 也就是改成 : return render(request, ‘posting.html’,locals()) 那我們來看這邊做了什麼 django.shortcuts.render call django.template.loader.render_to_string #12 http://imgur.com/rgUnbMu django.template.loader.get_template 拿到django.template.backends.django.Template 和上面一樣 但這邊有把request傳給render 造成make_context有拿到request所以回傳RequestContext 剩下的上面都講過了 : 結果錯誤訊息就不見了 : 我想要問各位大大,書上的寫法有錯嗎? : 如果有,要怎麼用RequestContext以及template.render()改到對? : 如果沒有,我是死在哪邊? xd --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.8.146
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Python/M.1501856704.A.1BC.html
1F:推 Sunal: 推推 08/04 22:42
2F:→ uranusjr: 欸其實上面那個也有傳 request, 也有呼叫 make_context 08/04 22:47
3F:→ uranusjr: 真正的重點在上下兩個 template.render 是不一樣的 08/04 22:47
4F:→ djshen: make_context的request是從Template.render裡面傳過來 08/04 22:48
5F:→ uranusjr: 所以這樣解釋原 po 應該會覺得怪, 但我也不知道該怎麼講 08/04 22:49
6F:→ uranusjr: 然後原本程式其實改 template.template.render(context) 08/04 22:49
7F:→ uranusjr: 就會過了我覺得要 trace 的話應該要解釋一下 XDDD 08/04 22:50
8F:→ djshen: html = template.render(request_context)這邊沒傳request 08/04 22:50
9F:→ djshen: 我覺得新手應該不需要了解那麼深啦.. 08/04 22:50
10F:→ djshen: 沒傳request造成傳給make_context的request也是None 08/04 22:51
11F:→ uranusjr: 是這樣沒錯啦, 不過原書亂挖坑我覺得講一半就不太好, 要 08/04 22:54
12F:→ uranusjr: 嘛就不要講要嘛就整個脈絡要搞清楚; 這整件事最讓我無法 08/04 22:55
13F:→ uranusjr: 理解的是這書竟然在第八頁講這個主題, 到底在搞什麼鬼 08/04 22:55
14F:推 Sunal: 其實django的tutorial對新手就很夠用了 08/04 22:59
15F:→ djshen: 看起來是1.10改的 https://goo.gl/MtJ3Z2 08/04 23:06
16F:→ djshen: 之前會檢查type 08/04 23:08
17F:推 Yshuan: 推 這篇超實用 08/06 01:58
18F:推 karta0910489: 推 08/06 02:45
19F:推 Aquatics: 推 10/17 16:20







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:iOS站內搜尋

TOP